<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"><title>JDK1.8新特性 | InsectMk的个人空间</title><meta name="keywords" content="Java,千锋教育,培训"><meta name="author" content="InsectMk,3067836615@qq.com"><meta name="copyright" content="InsectMk"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#f7f9fe"><meta name="mobile-web-app-capable" content="yes"><meta name="apple-touch-fullscreen" content="yes"><meta name="apple-mobile-web-app-title" content="JDK1.8新特性"><meta name="application-name" content="JDK1.8新特性"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="#f7f9fe"><meta property="og:type" content="article"><meta property="og:title" content="JDK1.8新特性"><meta property="og:url" content="https://insectmk.top/posts/d111bf74/index.html"><meta property="og:site_name" content="InsectMk的个人空间"><meta property="og:description" content="JDK1.8新特性JDK1.8新特性简介 速度更快 - 优化底层源码，比如HashMap、ConcurrentHashMap 代码更少 - 添加新的语法Lambda表达式 强大的Stream API 便于并行 最大化减少空指针异常 - Optional  Lambda表达式简介 Lambda是一个匿"><meta property="og:locale" content="zh-CN"><meta property="og:image" content="https://insectmk.top/static/img/head/insectmk.jpg"><meta property="article:author" content="InsectMk"><meta property="article:tag" content="[&quot;InsectMk&quot;,&quot;个人博客&quot;]"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://insectmk.top/static/img/head/insectmk.jpg"><meta name="description" content="JDK1.8新特性JDK1.8新特性简介 速度更快 - 优化底层源码，比如HashMap、ConcurrentHashMap 代码更少 - 添加新的语法Lambda表达式 强大的Stream API 便于并行 最大化减少空指针异常 - Optional  Lambda表达式简介 Lambda是一个匿"><link rel="shortcut icon" href="/static/img/website/favicon.jpg"><link rel="canonical" href="https://insectmk.top/posts/d111bf74/"><link rel="preconnect" href="//cdn.cbd.int"><link rel="preconnect" href="//busuanzi.ibruce.info"><meta name="google-site-verification" content="xxx"><meta name="baidu-site-verification" content="code-xxx"><meta name="msvalidate.01" content="xxx"><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.cbd.int/node-snackbar@0.1.16/dist/snackbar.min.css" media="print" onload='this.media="all"'><link rel="stylesheet" href="https://cdn.cbd.int/@fancyapps/ui@5.0.28/dist/fancybox/fancybox.css" media="print" onload='this.media="all"'><script>let GLOBAL_CONFIG={linkPageTop:{enable:!0,title:"与数百名博主无限进步",addFriendPlaceholder:"昵称（请勿包含博客等字样）：\n网站地址（要求博客地址，请勿提交个人主页）：\n头像图片url（请提供尽可能清晰的图片，我会上传到我自己的图床）：\n描述：\n站点截图（可选）：\n"},peoplecanvas:{enable:!0,img:"https://image.insectmk.cn/hexo-gitee-blog/article/_post/summary/hello-world/cover.jpg"},postHeadAiDescription:void 0,diytitle:void 0,LA51:void 0,greetingBox:void 0,twikooEnvId:"",commentBarrageConfig:void 0,root:"/",preloader:{source:3},friends_vue_info:void 0,navMusic:!0,mainTone:void 0,authorStatus:void 0,algolia:void 0,localSearch:{path:"/search.xml",preload:!0,languages:{hits_empty:"找不到您查询的内容：${query}"}},translate:{defaultEncoding:2,translateDelay:0,msgToTraditionalChinese:"繁",msgToSimplifiedChinese:"简",rightMenuMsgToTraditionalChinese:"转为繁体",rightMenuMsgToSimplifiedChinese:"转为简体"},noticeOutdate:void 0,highlight:{plugin:"highlight.js",highlightCopy:!0,highlightLang:!0,highlightHeightLimit:330},copy:{success:"复制成功",error:"复制错误",noSupport:"浏览器不支持"},relativeDate:{homepage:!1,simplehomepage:!0,post:!1},runtime:"天",date_suffix:{just:"刚刚",min:"分钟前",hour:"小时前",day:"天前",month:"个月前"},copyright:{copy:!0,copyrightEbable:!1,limitCount:50,languages:{author:"作者: InsectMk",link:"链接: ",source:"来源: InsectMk的个人空间",info:"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。",copySuccess:"复制成功，复制和转载请标注本文地址"}},lightbox:"fancybox",Snackbar:{chs_to_cht:"你已切换为繁体",cht_to_chs:"你已切换为简体",day_to_night:"你已切换为深色模式",night_to_day:"你已切换为浅色模式",bgLight:"#425AEF",bgDark:"#1f1f1f",position:"top-center"},source:{justifiedGallery:{js:"https://cdn.cbd.int/flickr-justified-gallery@2.1.2/dist/fjGallery.min.js",css:"https://cdn.cbd.int/flickr-justified-gallery@2.1.2/dist/fjGallery.css"}},isPhotoFigcaption:!1,islazyload:!1,isAnchor:!1,shortcutKey:void 0,autoDarkmode:!0}</script><script id="config-diff">var GLOBAL_CONFIG_SITE={configTitle:"InsectMk的个人空间",title:"JDK1.8新特性",postAI:"",pageFillDescription:"JDK1.8新特性, JDK1.8新特性简介, Lambda表达式, 简介, 基础语法, 注意点, 练习, 函数式接口, 简介, 四大核心函数式接口, 方法、构造方法和数组引用, 方法引用, 构造方法引用, 数组引用, Stream, 简介, 执行步骤, 创建Stream, 中间操作 - 筛选与切片, 中间操作 - 映射, 中间操作 - 排序, 终止操作 - 匹配与查找, 终止操作 - 归约, 终止操作 - 收集, 并行流与串行流, Optional, 接口的默认方法与静态方法, 日期组件, 组件简介, 日期时间类、时间戳、间隔类, 日期时间格式化类-DateTimeFormatter, 时间矫正器类-TemporalAdjuster, 时区类, 重复注解及类型注解新特性新特性简介速度更快优化底层源码比如代码更少添加新的语法表达式强大的便于并行最大化减少空指针异常表达式简介是一个匿名函数方法允许把函数作为一个方法的参数利用表达式可以写出更简洁更灵活的代码作为一种更紧凑的代码风格使的语言表达能力得到了提升一般都是优化匿名内部类基础语法无参数无返回值的抽象方法传统使用匿名内部类的方式采用表达式的方式无参数无返回值的抽象方法一个参数无返回值的抽象方法采用表达式的方式一个参数无返回值的抽象方法多个参数无返回值的抽象方法采用表达式的方式多个参数无返回值的抽象方法多个参数有返回值的抽象方法多个参数有返回值的抽象方法注意点重写方法的形参只有一个时可以不加小括号表达式当中不允许声明一个与局部变量同名的参数或者局部变量表达式中访问外层的局部变量外层的局部变量自动变成隐式常量默认添加重写方法的形参同时加类型或同时不加类型练习调用方法通过定制排序比较两个对象先按年龄比较年龄相同按照薪资比较使用表达式作为参数传递张三李四王五赵六孙七吴八课程枚举学生类创建接口创建抽象方法在测试类中编写方法使用接口作为参数将一个字符串转为大写并作为方法的返回值创建接口泛型为参数为返回值创建抽象方法在测试类中编写方法使用接口作为参数计算两个类型的和函数式接口简介函数式接口是指仅仅只包含一个抽象方法的接口提供了一个注解来定义函数式接口如果我们定义的接口不符合函数式的规范便会报错配合表达式一起使用四大核心函数式接口函数式接口参数类型返回类型用途消费型接口供给型接口函数型接口断言型接口对类型为参数应用操作包含方法为对类型为参数应用操作并返回类型的结果包含方法为对类型为的对象进行一元运算并返回类型的结果包含方法为对类型为的对象进行二元运算并返回类型的结果包含方法为分别计算值的函数参数为类型的函数应用场景当项目中需要一个接口并且该接口中只有一个抽象方法就没必要去创建新的接口直接选择提供的使用合适的函数式接口即可方法构造方法和数组引用方法构造方法和数组引用就是的另一种表现形式方法引用若表达式中的内容由方法已经实现了可以使用方法引用这个技能当你需要使用方法引用时目标引用放在分隔符前方法的名称放在后面对象实例方法表达式中调用方法的参数类型和返回值必须和函数式接口中的抽象方法一致里的参数列表和返回值类型必须和方法一致才行对象实例方法类名静态方法表达式中调用方法的参数类型和返回值必须和函数式接口中的抽象方法一致类名静态方法类名实例方法表达式参数列表中第一个参数必须是实例方法的调用者表达式参数列表中第二个参数必须是实例方法的参数类名实例方法注意表达式参数列表中第一个参数是方法的调用者表达式参数列表中第二个参数是方法的参数构造方法引用类名需要调用的构造方法的参数列表必须和函数式接口中抽象方法的参数列表一致需求创建学生对象桥本有菜桥本有菜课程枚举学生类数组引用语法格式创建数组简介流是数据渠道用于操作数据源集合数组等生成元素序列换言之集合是存储数据的容器流使用操作这些数据的可以对集合进行非常复杂的查找过滤映射数据等操作类似于执行数据库查询提供了一种高效且易于使用的处理数据的方式注意不会存储数据不会改变源数据通过一系列操作数据源会返回一个持有结果的新操作是延迟执行的意味着流会等到需要结果的时候才执行执行步骤创建通过数据源集合数组等获取一个中间操作中间操作链对源数据的数据进行处理终止操作执行中间操作并产生结果创建方式一通过接口提供的串行流或并行流获取流对象方式二通过的静态方法获取流对象方式三通过的静态方法获取流对象方式四创建无限流迭代生成注意多个中间操作可以连接成一个流水线除非流水线触发终止操作否则中间操作不会执行任何的处理而在终止操作时一次性全部处理称为惰性求值延迟加载中间操作筛选与切片方法描述从流中排除元素设置限制数据条数跳过元素返回跳过个元素的流若流中不满足个元素则返回空流与互补筛选流通过元素对象的和方法去除重复元素如果没有终止操作中间操作就不会被调用终止操作时一次性全部处理这种称为惰性求值延迟加载张三李四王五赵六孙七吴八李四需求过滤掉小于的学生对象中间操作迭代输出流里的数据就等同于终止操作迭代功能在中完成称为内部迭代集合使用称为外部迭代需求过滤掉小于的学生对象并显示条注意因为限制了数据条数所以满足数据条数后后续的操作就不再运行了效率就提高了短路迭代输出流里的数据就等同于终止操作迭代功能在中完成称为内部迭代集合使用称为外部迭代需求过滤掉小于的学生对象并跳过第个学生对象迭代输出流里的数据就等同于终止操作迭代功能在中完成称为内部迭代集合使用称为外部迭代需求过滤掉小于的学生对象并筛选掉重复元素底层通过元素对象对象的和方法去除重复元素迭代输出流里的数据就等同于终止操作迭代功能在中完成称为内部迭代集合使用称为外部迭代课程枚举学生类中间操作映射方法描述将流中所有元素映射成一个新的元素或者提取信息将流中的流整合整合成平面平铺流张三李四王五赵六孙七吴八张三李四王五赵六孙七吴八李四将流中所有元素映射成一个新的元素或者提取信息方式映射成一个新的元素需求需求获取流对象打印出所有学生的姓氏方式映射成提取信息需求把原来流中的学生对象替换成学生姓名带着需求学将流中的流整合整合成平面平铺流需求将里的字符串转换为字符输出解决方案使用完成需求可以看到流里包含另外的流非常麻烦张三李四解决方案使用完成需求将流中的流一并整合张三李四将字符串拆分出字符转换为流的方法课程枚举学生类中间操作排序方法解释使用元素原有排序规则使用自定义排序规则张三李四王五赵六孙七吴八吴八使用元素原有排序规则需求按照年龄排序使用自定义排序规则需求按照工资排序课程枚举学生类终止操作匹配与查找方法描述检查是否匹配所有元素检查是否匹配至少一个元素检查是否没有匹配所有元素返回第一个元素返回任意一个元素但效果不好返回流中元素的个数返回流中最大值返回流中最小值张三李四王五赵六孙七吴八吴八需求检查流中素所有元素是否匹配工资需求检查流中素所有元素至少匹配一个工资需求检查流中素所有元素是否没有匹配工资需求返回工资最高的学生信息这种写法防止出现需求返回随机学生信息但效果不好需求获取学生个数需求获取最高工资的学生信息需求获取最工资的学生信息课程枚举学生类终止操作归约归约将流中的元素反复结合起来得到一个值的连接通常称为模式因用它进行网络搜索而出名方法描述参数初始值结合逻辑参数结合逻辑张三李四王五赵六孙七吴八李四需求获取集合中元素的总和需求获取集合中所有学生工资总和课程枚举学生类终止操作收集收集将流转换为其他形式接收一个接口的实现用于给中元素做汇总的方法方法描述把元素放入集合中张三李四王五赵六孙七吴八李四把数据收集到集合中需求把当前学生姓名提取出来并把数据放入集合中需求把当前学生姓名提取出来并把数据放入集合中需求把当前学生姓名提取出来并把数据放入指定集合中收集流中的各种数据需求收集获取学生个数需求收集获取学生平均工资需求收集获取学生总工资需求收集获取学生工资最大值需求收集获取学生工资最小值需求收集获取工资最多的学生信息需求收集获取工资最多的学生信息分组需求按照学科分组多级分组需求按照学科分组在按照年龄分组青年中年老年分区需求按照工资为标准分区获取元素中字段的各种信息需求获取学生工资信息再获取总值平均值最大值最小值拼接信息需求拼接学生姓名课程枚举学生类并行流与串行流并行流就是把一个内容拆分成多个数据块并用不同的线程分别处理每个数据块的流中将并行进行了优化我们可以很容易的对数据进行并行操作可以声明性地通过并行流与顺序流之间进行切换注意默认为顺序流串行流并行流一般在大数据搜索里使用到之前也有并行流叫做并行计算框架需求使用并行流计算之和生成的数流转换为并行流求和类是一个容器类代表一个存在或不存在的值原来用表示一个值不存在现在可以更好的表达这个概念并且可以避免空指针异常此类的设计就是更好的避免空指针异常方法描述创建一个实例创建一个空的实例若不为创建实例否则创建空实例获取实例里的对象判断是否包含值如果调用对象包含值返回该值否则返回如果调用对象包含值返回该值否则返回获取的值如果有值对其处理并返回处理后的否则返回与类似要求返回值必须是创建一个实例把对象封装到容器里创建一个空的容器创建一个实例若对象为创建空实例若对象为不为创建实例判断容器里是否包含对象包含获取容器里的对象不包含容器里的对象为如果容器里有对象就返回否则就返回新对象不同情况下可以返回不同对象比可变性更强吴彦祖麻生希获取原容器中的某个值并返回新容器中与类似要求返回值必须是课程枚举学生类接口的默认方法与静态方法从开始接口中可以有默认方法既修饰的方法此方法可以让接口的实现类所调用而接口中的静态方法直接用接口名调用即可接口中的默认方法接口中的静态方法接口默认方法的类优先原则如果一个接口中定义了一个默认方法而接口实现类的父类定义了一个同名的方法时选择父类中的方法接口冲突如果一个父接口提供一个默认方法而另一个接口也提供了一个具有相同名称和参数列表的方法不管方法是否是默认方法那么必须覆盖该方法来解决冲突接口中的默认方法类中的默认方法接口中的默认方法接口中的默认方法日期组件提供的新日期类都是不可变的既不管怎么样的改变都会产生一个新的实例他们都是线程安全的日期组件遵循与世界时间标准组件简介包路径类名描述针对日期和时间操作的包用于表示日期的类用于表示时间的类用于表示日期时间的类时间戳类到线程的毫秒数两个日期间隔类两个时间间隔类针对日期时间特殊格式操作的包日本帝国历法系统类泰国佛教日历系统类针对时间日期时间格式化操作的包格式化日期时间类针对时间矫正操作的包针对时区操作的包日期时间类时间戳间隔类这三个日期类的使用大致一样获取当前日期时间对象获取指定日期时间对象获取推后的时间日期对象获取提前的时间日期对象使用时间戳从年月日到现在的毫秒值默认创建世界标准时间时区的时间戳对象获取偏移小时的偏移日期时间对象获取时间戳的毫秒值形式获取一个年月日往后退秒的时间戳对象时间间隔类获取时间间隔类对象获取时间间隔类对象日期间隔类日期时间格式化类格式化日期时间类获取本地标准的日期时间格式化对象格式化时间日期自定义日期时间格式化对象年月日格式化时间日期将指定格式的字符串解析成对象年月日时间矫正器类时间矫正器设置指定月份设置下一个周末自定义时间矫正器设置下一个工作周五周六时区类时区类获取所有时区字符串获取指定时区的日期时间对象获取指定时区的日期时间对象偏移量重复注解及类型注解开始可以重复注解一个类可有多个同样的注解重复注解何老师苍老师需求获取的多个作者注解需求获取方法中参数里的注解获取参数中注解列表有可能一个参数多个注解获取具体注解波老师作者注解的容器作者注解类型注解作用在参数",isPost:!0,isHome:!1,isHighlightShrink:!1,isToc:!0,postUpdate:"2023-06-26 20:24:46",postMainColor:""}</script><noscript><style type="text/css">#nav{opacity:1}.justified-gallery img{opacity:1}#post-meta time,#recent-posts time{display:inline!important}</style></noscript><script>(e=>{e.saveToLocal={set:(e,t,a)=>{var o;0!==a&&(o=Date.now(),localStorage.setItem(e,JSON.stringify({value:t,expiry:o+864e5*a})))},get:e=>{var t=localStorage.getItem(e);if(t){t=JSON.parse(t);if(!(Date.now()>t.expiry))return t.value;localStorage.removeItem(e)}}},e.getScript=(o,c={})=>new Promise((t,e)=>{let a=document.createElement("script");a.src=o,a.async=!0,a.onerror=e,a.onload=a.onreadystatechange=function(){var e=this.readyState;e&&"loaded"!==e&&"complete"!==e||(a.onload=a.onreadystatechange=null,t())},Object.keys(c).forEach(e=>{a.setAttribute(e,c[e])}),document.head.appendChild(a)}),e.getCSS=(o,c=!1)=>new Promise((t,e)=>{let a=document.createElement("link");a.rel="stylesheet",a.href=o,c&&(a.id=c),a.onerror=e,a.onload=a.onreadystatechange=function(){var e=this.readyState;e&&"loaded"!==e&&"complete"!==e||(a.onload=a.onreadystatechange=null,t())},document.head.appendChild(a)}),e.activateDarkMode=()=>{document.documentElement.setAttribute("data-theme","dark"),null!==document.querySelector('meta[name="theme-color"]')&&document.querySelector('meta[name="theme-color"]').setAttribute("content","#18171d")},e.activateLightMode=()=>{document.documentElement.setAttribute("data-theme","light"),null!==document.querySelector('meta[name="theme-color"]')&&document.querySelector('meta[name="theme-color"]').setAttribute("content","#f7f9fe")};var e=saveToLocal.get("theme"),t=window.matchMedia("(prefers-color-scheme: dark)").matches,a=window.matchMedia("(prefers-color-scheme: light)").matches,o=window.matchMedia("(prefers-color-scheme: no-preference)").matches,c=!t&&!a&&!o,t=(void 0===e?(a?activateLightMode():t?activateDarkMode():(o||c)&&((a=(new Date).getHours())<=6||18<=a?activateDarkMode:activateLightMode)(),window.matchMedia("(prefers-color-scheme: dark)").addListener(e=>{void 0===saveToLocal.get("theme")&&(e.matches?activateDarkMode:activateLightMode)()})):("light"===e?activateLightMode:activateDarkMode)(),saveToLocal.get("aside-status"));void 0!==t&&("hide"===t?document.documentElement.classList.add("hide-aside"):document.documentElement.classList.remove("hide-aside"));/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)&&document.documentElement.classList.add("apple")})(window)</script><meta name="generator" content="Hexo 7.3.0"><link rel="alternate" href="/atom.xml" title="InsectMk的个人空间" type="application/atom+xml"><link rel="alternate" href="/rss2.xml" title="InsectMk的个人空间" type="application/rss+xml"></head><body data-type="anzhiyu"><div id="web_bg"></div><div id="an_music_bg"></div><div id="loading-box" onclick="document.getElementById(&quot;loading-box&quot;).classList.add(&quot;loaded&quot;)"><div class="loading-bg"><img class="loading-img nolazyload" alt="加载头像" src="/static/img/head/insectmk.jpg"><div class="loading-image-dot"></div></div></div><script>let preloader={endLoading:()=>{document.getElementById("loading-box").classList.add("loaded")},initLoading:()=>{document.getElementById("loading-box").classList.remove("loaded")}};window.addEventListener("load",()=>{preloader.endLoading()}),setTimeout(function(){preloader.endLoading()},1e4),document.addEventListener("pjax:send",()=>{preloader.initLoading()}),document.addEventListener("pjax:complete",()=>{preloader.endLoading()})</script><link rel="stylesheet" href="https://cdn.cbd.int/anzhiyu-theme-static@1.1.10/progress_bar/progress_bar.css"><script async src="https://cdn.cbd.int/pace-js@1.2.4/pace.min.js" data-pace-options="{ &quot;restartOnRequestAfter&quot;:false,&quot;eventLag&quot;:false}"></script><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><div id="nav-group"><span id="blog_name"><div class="back-home-button"><i class="anzhiyufont anzhiyu-icon-grip-vertical"></i><div class="back-menu-list-groups"><div class="back-menu-list-group"><div class="back-menu-list-title">网页</div><div class="back-menu-list"><a class="back-menu-item" target="_blank" rel="noopener" href="https://insectmk.cn/" title="主博客"><img class="back-menu-item-icon" src="/static/img/website/favicon.jpg" alt="主博客"><span class="back-menu-item-text">主博客</span></a><a class="back-menu-item" href="https://insectmk.top/" title="次博客"><img class="back-menu-item-icon" src="/static/img/website/favicon.jpg" alt="次博客"><span class="back-menu-item-text">次博客</span></a></div></div><div class="back-menu-list-group"><div class="back-menu-list-title">项目</div><div class="back-menu-list"><a class="back-menu-item" target="_blank" rel="noopener" href="https://gitee.com/insectmk/chatbot-web" title="智能聊天机器人网站"><img class="back-menu-item-icon" src="https://image.insectmk.cn/hexo-gitee-blog/project/icon/chatbot-web.jpg" alt="智能聊天机器人网站"><span class="back-menu-item-text">智能聊天机器人网站</span></a></div></div></div></div><a id="site-name" href="/" accesskey="h"><div class="title">InsectMk的个人空间</div><i class="anzhiyufont anzhiyu-icon-house-chimney"></i></a></span><div class="mask-name-container"><div id="name-container"><a id="page-name" href="javascript:anzhiyu.scrollToDest(0, 500)">PAGE_NAME</a></div></div><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-home"></use></svg><span>主页</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-hand-peace"></use></svg><span>娱乐</span></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/entertainment/gallery/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-images"></use></svg><span>图库</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-comments"></use></svg><span>交流</span></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/contact/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-paper-plane"></use></svg><span>留言板</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/contact/link/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-link"></use></svg><span>友链</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-compass"></use></svg><span>目录</span></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/categories/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-archive"></use></svg><span>分类</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/tags/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-tags"></use></svg><span>标签</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/archives/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-folder-open"></use></svg><span>归档</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-server"></use></svg><span>服务</span></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/server/left4dead2/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-icons-left__dead_"></use></svg><span>求生之路</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/server/minecraft/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-minecraft"></use></svg><span>我的世界</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-clipboard"></use></svg><span>日志</span></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/log/website/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-website-with-different-sections"></use></svg><span>个人网站</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/log/pc/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-desktop"></use></svg><span>个人电脑</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-bars"></use></svg><span>其他</span></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/extend/remind/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-note"></use></svg><span>备忘</span></a></li></ul></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-id-card"></use></svg><span>关于</span></a></div></div></div><div id="nav-right"><div class="nav-button" id="randomPost_button"><a class="site-page" onclick="toRandomPost()" title="随机前往一个文章" href="javascript:void(0);"><i class="anzhiyufont anzhiyu-icon-dice"></i></a></div><div class="nav-button" id="search-button"><a class="site-page social-icon search" href="javascript:void(0);" title="搜索🔍" accesskey="s"><i class="anzhiyufont anzhiyu-icon-magnifying-glass"></i><span> 搜索</span></a></div><input id="center-console" type="checkbox"><label class="widget" for="center-console" title="中控台" onclick="anzhiyu.switchConsole()"><i class="left"></i><i class="widget center"></i><i class="widget right"></i></label><div id="console"><div class="console-card-group-reward"><ul class="reward-all console-card"><li class="reward-item"><a href="/static/img/website/reward/wechat.png" target="_blank"><img class="post-qr-code-img" alt="微信" src="/static/img/website/reward/wechat.png"></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/static/img/website/reward/alipay.png" target="_blank"><img class="post-qr-code-img" alt="支付宝" src="/static/img/website/reward/alipay.png"></a><div class="post-qr-code-desc">支付宝</div></li></ul></div><div class="console-card-group"><div class="console-card-group-left"><div class="console-card" id="card-newest-comments"><div class="card-content"><div class="author-content-item-tips">互动</div><span class="author-content-item-title">最新评论</span></div><div class="aside-list"><span>正在加载中...</span></div></div></div><div class="console-card-group-right"><div class="console-card tags"><div class="card-content"><div class="author-content-item-tips">兴趣点</div><span class="author-content-item-title">寻找你感兴趣的领域</span><div class="card-tags"><div class="item-headline"></div><div class="card-tag-cloud"><a href="/tags/AOP/" style="font-size:1.05rem">AOP<sup>1</sup></a><a href="/tags/Butterfly/" style="font-size:1.05rem">Butterfly<sup>4</sup></a><a href="/tags/Docker/" style="font-size:1.05rem">Docker<sup>6</sup></a><a href="/tags/ElementUI/" style="font-size:1.05rem">ElementUI<sup>1</sup></a><a href="/tags/Frp/" style="font-size:1.05rem">Frp<sup>1</sup></a><a href="/tags/Hexo/" style="font-size:1.05rem">Hexo<sup>8</sup></a><a href="/tags/IntelliJ-IDEA/" style="font-size:1.05rem">IntelliJ IDEA<sup>1</sup></a><a href="/tags/JDK/" style="font-size:1.05rem">JDK<sup>1</sup></a><a href="/tags/JWT/" style="font-size:1.05rem">JWT<sup>1</sup></a><a href="/tags/Java/" style="font-size:1.05rem">Java<sup>35</sup></a><a href="/tags/Junit/" style="font-size:1.05rem">Junit<sup>1</sup></a><a href="/tags/Leanote/" style="font-size:1.05rem">Leanote<sup>1</sup></a><a href="/tags/Maven/" style="font-size:1.05rem">Maven<sup>2</sup></a><a href="/tags/MyBatisPlus/" style="font-size:1.05rem">MyBatisPlus<sup>1</sup></a><a href="/tags/MySQL5-7/" style="font-size:1.05rem">MySQL5.7<sup>1</sup></a><a href="/tags/Mybatis/" style="font-size:1.05rem">Mybatis<sup>1</sup></a><a href="/tags/MybatisPlus/" style="font-size:1.05rem">MybatisPlus<sup>1</sup></a><a href="/tags/OSS/" style="font-size:1.05rem">OSS<sup>1</sup></a><a href="/tags/Spring/" style="font-size:1.05rem">Spring<sup>2</sup></a><a href="/tags/Spring-Boot/" style="font-size:1.05rem">SpringBoot<sup>6</sup></a><a href="/tags/Vue2/" style="font-size:1.05rem">Vue2<sup>9</sup></a><a href="/tags/Vue3/" style="font-size:1.05rem">Vue3<sup>1</sup></a><a href="/tags/Vuetify/" style="font-size:1.05rem">Vuetify<sup>1</sup></a><a href="/tags/Vuex/" style="font-size:1.05rem">Vuex<sup>1</sup></a><a href="/tags/Waline/" style="font-size:1.05rem">Waline<sup>3</sup></a><a href="/tags/mvc/" style="font-size:1.05rem">mvc<sup>3</sup></a><a href="/tags/mybatis/" style="font-size:1.05rem">mybatis<sup>1</sup></a><a href="/tags/profile/" style="font-size:1.05rem">profile<sup>1</sup></a><a href="/tags/spring/" style="font-size:1.05rem">spring<sup>5</sup></a><a href="/tags/springboot/" style="font-size:1.05rem">springboot<sup>4</sup></a><a href="/tags/transaction/" style="font-size:1.05rem">transaction<sup>1</sup></a><a href="/tags/yaml/" style="font-size:1.05rem">yaml<sup>2</sup></a><a href="/tags/%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2/" style="font-size:1.05rem">个人博客<sup>11</sup></a><a href="/tags/%E4%BA%8B%E5%8A%A1/" style="font-size:1.05rem">事务<sup>1</sup></a><a href="/tags/%E5%88%86%E9%A1%B5/" style="font-size:1.05rem">分页<sup>1</sup></a><a href="/tags/%E5%89%8D%E7%AB%AF/" style="font-size:1.05rem">前端<sup>1</sup></a><a href="/tags/%E6%9C%8D%E5%8A%A1%E5%99%A8/" style="font-size:1.05rem">服务器<sup>20</sup></a><a href="/tags/%E6%AF%95%E4%B8%9A%E8%AE%BE%E8%AE%A1/" style="font-size:1.05rem">毕业设计<sup>14</sup></a><a href="/tags/%E7%A6%85%E9%81%93/" style="font-size:1.05rem">禅道<sup>1</sup></a><a href="/tags/%E7%BB%BF%E8%89%B2%E8%A7%A3%E5%8E%8B/" style="font-size:1.05rem">绿色解压<sup>1</sup></a></div></div><hr></div></div><div class="console-card history"><div class="item-headline"><i class="anzhiyufont anzhiyu-icon-box-archiv"></i><span>文章</span></div><div class="card-archives"><div class="item-headline"><i class="anzhiyufont anzhiyu-icon-archive"></i><span>归档</span><a class="card-more-btn" href="/archives/" title="查看更多"> <i class="anzhiyufont anzhiyu-icon-angle-right"></i></a></div><ul class="card-archive-list"><li class="card-archive-list-item"><a class="card-archive-list-link" href="/archives/2024/07/"><span class="card-archive-list-date">七月 2024</span><div class="card-archive-list-count-group"><span class="card-archive-list-count">4</span><span>篇</span></div></a></li><li class="card-archive-list-item"><a class="card-archive-list-link" href="/archives/2024/06/"><span class="card-archive-list-date">六月 2024</span><div class="card-archive-list-count-group"><span class="card-archive-list-count">1</span><span>篇</span></div></a></li><li class="card-archive-list-item"><a class="card-archive-list-link" href="/archives/2024/05/"><span class="card-archive-list-date">五月 2024</span><div class="card-archive-list-count-group"><span class="card-archive-list-count">4</span><span>篇</span></div></a></li><li class="card-archive-list-item"><a class="card-archive-list-link" href="/archives/2024/04/"><span class="card-archive-list-date">四月 2024</span><div class="card-archive-list-count-group"><span class="card-archive-list-count">8</span><span>篇</span></div></a></li><li class="card-archive-list-item"><a class="card-archive-list-link" href="/archives/2024/03/"><span class="card-archive-list-date">三月 2024</span><div class="card-archive-list-count-group"><span class="card-archive-list-count">9</span><span>篇</span></div></a></li><li class="card-archive-list-item"><a class="card-archive-list-link" href="/archives/2024/02/"><span class="card-archive-list-date">二月 2024</span><div class="card-archive-list-count-group"><span class="card-archive-list-count">5</span><span>篇</span></div></a></li><li class="card-archive-list-item"><a class="card-archive-list-link" href="/archives/2024/01/"><span class="card-archive-list-date">一月 2024</span><div class="card-archive-list-count-group"><span class="card-archive-list-count">5</span><span>篇</span></div></a></li><li class="card-archive-list-item"><a class="card-archive-list-link" href="/archives/2023/10/"><span class="card-archive-list-date">十月 2023</span><div class="card-archive-list-count-group"><span class="card-archive-list-count">8</span><span>篇</span></div></a></li></ul></div><hr></div></div></div><div class="button-group"><div class="console-btn-item"><a class="darkmode_switchbutton" title="显示模式切换" href="javascript:void(0);"><i class="anzhiyufont anzhiyu-icon-moon"></i></a></div><div class="console-btn-item" id="consoleHideAside" onclick="anzhiyu.hideAsideBtn()" title="边栏显示控制"><a class="asideSwitch"><i class="anzhiyufont anzhiyu-icon-arrows-left-right"></i></a></div><div class="console-btn-item" id="consoleMusic" onclick="anzhiyu.musicToggle()" title="音乐开关"><a class="music-switch"><i class="anzhiyufont anzhiyu-icon-music"></i></a></div></div><div class="console-mask" onclick="anzhiyu.hideConsole()" href="javascript:void(0);"></div></div><div class="nav-button" id="nav-totop"><a class="totopbtn" href="javascript:void(0);"><i class="anzhiyufont anzhiyu-icon-arrow-up"></i><span id="percent" onclick="anzhiyu.scrollToDest(0,500)">0</span></a></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);" title="切换"><i class="anzhiyufont anzhiyu-icon-bars"></i></a></div></div></div></nav></header><main id="blog-container"><div class="layout" id="content-inner"><div id="post"><div id="post-info"><div id="post-firstinfo"><div class="meta-firstline"><a class="post-meta-original">原创</a><span class="post-meta-categories"><span class="post-meta-separator"></span><i class="anzhiyufont anzhiyu-icon-inbox post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%95%99%E8%82%B2/" itemprop="url">教育</a></span><span class="article-meta tags"><a class="article-meta__tags" href="/tags/Java/" tabindex="-1" itemprop="url"> <span><i class="anzhiyufont anzhiyu-icon-hashtag"></i>Java</span></a><a class="article-meta__tags" href="/tags/%E5%8D%83%E9%94%8B%E6%95%99%E8%82%B2/" tabindex="-1" itemprop="url"> <span><i class="anzhiyufont anzhiyu-icon-hashtag"></i>千锋教育</span></a><a class="article-meta__tags" href="/tags/%E5%9F%B9%E8%AE%AD/" tabindex="-1" itemprop="url"> <span><i class="anzhiyufont anzhiyu-icon-hashtag"></i>培训</span></a></span></div></div><h1 class="post-title" itemprop="name headline">JDK1.8新特性</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="anzhiyufont anzhiyu-icon-calendar-days post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" itemprop="dateCreated datePublished" datetime="2023-06-26T12:24:46.000Z" title="发表于 2023-06-26 20:24:46">2023-06-26</time><span class="post-meta-separator"></span><i class="anzhiyufont anzhiyu-icon-history post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" itemprop="dateCreated datePublished" datetime="2023-06-26T12:24:46.000Z" title="更新于 2023-06-26 20:24:46">2023-06-26</time></span></div><div class="meta-secondline"><span class="post-meta-separator"></span><span class="post-meta-wordcount"><i class="anzhiyufont anzhiyu-icon-file-word post-meta-icon" title="文章字数"></i><span class="post-meta-label" title="文章字数">字数总计:</span><span class="word-count" title="文章字数">8.3k</span><span class="post-meta-separator"></span><i class="anzhiyufont anzhiyu-icon-clock post-meta-icon" title="阅读时长"></i><span class="post-meta-label" title="阅读时长">阅读时长:</span><span>36分钟</span></span><span class="post-meta-separator"></span><span data-flag-title="JDK1.8新特性"><i class="anzhiyufont anzhiyu-icon-fw-eye post-meta-icon"></i><span class="post-meta-label" title="阅读量">阅读量:</span><span class="waline-pageview-count" data-path="/posts/d111bf74/" title="访问量"><i class="anzhiyufont anzhiyu-icon-spinner anzhiyu-spin"></i></span></span><span class="post-meta-separator"> </span><span class="post-meta-position" title="作者IP属地为成都"><i class="anzhiyufont anzhiyu-icon-location-dot"></i>成都</span><span class="post-meta-separator"></span><span class="post-meta-commentcount"><i class="anzhiyufont anzhiyu-icon-comments post-meta-icon"></i><span class="post-meta-label">评论数:</span><a href="/posts/d111bf74/#post-comment"><span class="waline-comment-count" data-path="/posts/d111bf74/"><i class="anzhiyufont anzhiyu-icon-spinner anzhiyu-spin"></i></span></a></span></div></div></div><article class="post-content" id="article-container" itemscope itemtype="https://insectmk.top/posts/d111bf74/"><header><a class="post-meta-categories" href="/categories/%E6%95%99%E8%82%B2/" itemprop="url">教育</a><a href="/tags/Java/" tabindex="-1" itemprop="url">Java</a><a href="/tags/%E5%8D%83%E9%94%8B%E6%95%99%E8%82%B2/" tabindex="-1" itemprop="url">千锋教育</a><a href="/tags/%E5%9F%B9%E8%AE%AD/" tabindex="-1" itemprop="url">培训</a><h1 id="CrawlerTitle" itemprop="name headline">JDK1.8新特性</h1><span itemprop="author" itemscope itemtype="http://schema.org/Person">InsectMk</span><time itemprop="dateCreated datePublished" datetime="2023-06-26T12:24:46.000Z" title="发表于 2023-06-26 20:24:46">2023-06-26</time><time itemprop="dateCreated datePublished" datetime="2023-06-26T12:24:46.000Z" title="更新于 2023-06-26 20:24:46">2023-06-26</time></header><h1 id="JDK1-8新特性"><a href="#JDK1-8新特性" class="headerlink" title="JDK1.8新特性"></a>JDK1.8新特性</h1><h3 id="JDK1-8新特性简介"><a href="#JDK1-8新特性简介" class="headerlink" title="JDK1.8新特性简介"></a>JDK1.8新特性简介</h3><ul><li>速度更快 - 优化底层源码，比如HashMap、ConcurrentHashMap</li><li>代码更少 - 添加新的语法Lambda表达式</li><li>强大的Stream API</li><li>便于并行</li><li>最大化减少空指针异常 - Optional</li></ul><h3 id="Lambda表达式"><a href="#Lambda表达式" class="headerlink" title="Lambda表达式"></a>Lambda表达式</h3><h4 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h4><blockquote><p>Lambda是一个匿名函数(方法)， 允许把函数作为一个方法的参数 。利用Lambda表达式可以写出更简洁、更灵活的代码。作为一种更紧凑的代码风格，使Java的语言表达能力得到了提升。</p><p>一般都是优化匿名内部类</p></blockquote><h4 id="基础语法"><a href="#基础语法" class="headerlink" title="基础语法"></a>基础语法</h4><p><strong>无参数、无返回值的抽象方法</strong></p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line"><span class="comment">//		I1 i1 = new I1() &#123;</span></span><br><span class="line"><span class="comment">//			@Override</span></span><br><span class="line"><span class="comment">//			public void method() &#123;</span></span><br><span class="line"><span class="comment">//				System.out.println(&quot;传统使用匿名内部类的方式&quot;);</span></span><br><span class="line"><span class="comment">//			&#125;</span></span><br><span class="line"><span class="comment">//		&#125;;</span></span><br><span class="line"><span class="comment">//		i1.method();</span></span><br><span class="line"></span><br><span class="line">		<span class="type">I1</span> <span class="variable">i1</span> <span class="operator">=</span> ()-&gt; System.out.println(<span class="string">&quot;采用Lambda表达式的方式&quot;</span>);</span><br><span class="line">		i1.method();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">I1</span>&#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span>;<span class="comment">//无参数、无返回值的抽象方法</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong>一个参数、无返回值的抽象方法</strong></p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="type">I1</span> <span class="variable">i1</span> <span class="operator">=</span> (x)-&gt; System.out.println(<span class="string">&quot;采用Lambda表达式的方式 &quot;</span> + x);</span><br><span class="line">		i1.method(<span class="number">1000</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">I1</span>&#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">(<span class="type">int</span> num1)</span>;<span class="comment">//一个参数、无返回值的抽象方法</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong>多个参数、无返回值的抽象方法</strong></p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="type">I1</span> <span class="variable">i1</span> <span class="operator">=</span> (x,y,z)-&gt; </span><br><span class="line">        System.out.println(<span class="string">&quot;采用Lambda表达式的方式 &quot;</span> + x + y + z);</span><br><span class="line">        i1.method(<span class="number">1000</span>,<span class="number">2000</span>,<span class="number">3000</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">I1</span>&#123;</span><br><span class="line">    <span class="comment">//多个参数、无返回值的抽象方法</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">(<span class="type">int</span> num1,<span class="type">int</span> num2,<span class="type">int</span> num3)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong>多个参数、有返回值的抽象方法</strong></p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="type">I1</span> <span class="variable">i1</span> <span class="operator">=</span> (x,y,z)-&gt; x+y+z;</span><br><span class="line">		<span class="type">int</span> <span class="variable">result</span> <span class="operator">=</span> i1.method(<span class="number">1000</span>,<span class="number">2000</span>,<span class="number">3000</span>);</span><br><span class="line">		System.out.println(result);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">I1</span>&#123;</span><br><span class="line">	<span class="comment">//多个参数、有返回值的抽象方法</span></span><br><span class="line">	<span class="keyword">public</span> <span class="type">int</span> <span class="title function_">method</span><span class="params">(<span class="type">int</span> num1,<span class="type">int</span> num2,<span class="type">int</span> num3)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="注意点"><a href="#注意点" class="headerlink" title="注意点"></a>注意点</h4><ol><li>重写方法的形参只有一个时，可以不加小括号</li><li>Lambda表达式当中不允许声明一个与局部变量同名的参数或者局部变量</li><li>Lambda表达式中访问外层的局部变量，外层的局部变量自动变成隐式常量，默认添加final</li><li>重写方法的形参同时加类型或同时不加类型</li></ol><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="type">int</span> x;</span><br><span class="line">		<span class="type">int</span> <span class="variable">num</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">		<span class="type">I1</span> <span class="variable">i1</span> <span class="operator">=</span> x -&gt; System.out.println(x + (num++));</span><br><span class="line">        i1.method(<span class="number">1000</span>);</span><br><span class="line">		</span><br><span class="line">		<span class="type">I2</span> <span class="variable">i2</span> <span class="operator">=</span> (<span class="type">int</span> x,<span class="type">int</span> y) -&gt; &#123;</span><br><span class="line">			<span class="type">int</span> <span class="variable">result</span> <span class="operator">=</span> x+y;</span><br><span class="line">			<span class="keyword">return</span> result;</span><br><span class="line">		&#125;;</span><br><span class="line">		<span class="type">int</span> <span class="variable">result</span> <span class="operator">=</span> i2.method(<span class="number">10</span>, <span class="number">20</span>);</span><br><span class="line">		System.out.println(result);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">I1</span>&#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">(<span class="type">int</span> num1)</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">I2</span>&#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="type">int</span> <span class="title function_">method</span><span class="params">(<span class="type">int</span> num1,<span class="type">int</span> num2)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="练习"><a href="#练习" class="headerlink" title="练习"></a>练习</h4><p>1.调用Collections.sort()方法，通过定制排序比较两个Student对象（先按年龄比较，年龄相同按照薪资比较），使用Lambda表达式作为参数传递</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line">		List&lt;Student&gt; stuList = Arrays.asList(</span><br><span class="line">				<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;张三&quot;</span>, <span class="number">28</span>, <span class="number">4800</span>,Course.JAVA),<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;李四&quot;</span>, <span class="number">36</span>, <span class="number">7200</span>,Course.JAVA),<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;王五&quot;</span>, <span class="number">19</span>, <span class="number">9600</span>,Course.HTML),<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;赵六&quot;</span>, <span class="number">42</span>, <span class="number">6100</span>,Course.HTML),<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;孙七&quot;</span>, <span class="number">23</span>, <span class="number">9600</span>,Course.PYTHON),<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;吴八&quot;</span>, <span class="number">31</span>, <span class="number">3000</span>,Course.PYTHON));</span><br><span class="line">		</span><br><span class="line">		Collections.sort(stuList, (a,b)-&gt; &#123;</span><br><span class="line">			<span class="keyword">if</span>(a.getAge() == b.getAge())&#123;</span><br><span class="line">				<span class="keyword">return</span> Double.compare(a.getSalary(),b.getSalary());</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">return</span> a.getAge()-b.getAge();</span><br><span class="line">		&#125;);</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">for</span> (Student stu : stuList) &#123;</span><br><span class="line">			System.out.println(stu);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">enum</span> <span class="title class_">Course</span>&#123;<span class="comment">//课程枚举</span></span><br><span class="line">	JAVA,HTML,PYTHON;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span>&#123;<span class="comment">//学生类</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">private</span> String name;</span><br><span class="line">	<span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line">	<span class="keyword">private</span> <span class="type">double</span> salary;</span><br><span class="line">	<span class="keyword">private</span> Course course;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>2.创建I1接口，创建抽象方法：public String getValue(String str),在测试类中编写方法使用接口作为参数，将一个字符串转为大写，并作为方法的返回值</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="type">String</span> <span class="variable">strHandler</span> <span class="operator">=</span> strHandler(<span class="string">&quot;abc&quot;</span>, x-&gt; x.toUpperCase());</span><br><span class="line">		System.out.println(strHandler);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> String <span class="title function_">strHandler</span><span class="params">(String str,I1 i1)</span>&#123;</span><br><span class="line">		<span class="keyword">return</span> i1.getValue(str);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">I1</span>&#123;</span><br><span class="line">	<span class="keyword">public</span> String <span class="title function_">getValue</span><span class="params">(String str)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>3.创建I1&lt;T,R&gt;接口，泛型T为参数，R为返回值，创建抽象方法：public R add(T t1,T t2),在测试类中编写方法使用接口作为参数，计算两个long类型的和</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="type">Long</span> <span class="variable">addLong</span> <span class="operator">=</span> addLong(<span class="number">100L</span>, <span class="number">200L</span>, (x,y)-&gt; x+y);</span><br><span class="line">		System.out.println(addLong);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> Long <span class="title function_">addLong</span><span class="params">(Long l1,Long l2,I1&lt;Long,Long&gt; i1)</span>&#123;</span><br><span class="line">		<span class="keyword">return</span> i1.add(l1, l2);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">I1</span>&lt;T,R&gt;&#123;</span><br><span class="line">	<span class="keyword">public</span> R <span class="title function_">add</span><span class="params">(T t1,T t2)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="函数式接口"><a href="#函数式接口" class="headerlink" title="函数式接口"></a>函数式接口</h3><h4 id="简介-1"><a href="#简介-1" class="headerlink" title="简介"></a>简介</h4><blockquote><p>函数式接口是指仅仅只包含一个抽象方法的接口，jdk1.8提供了一个@FunctionalInterface注解来定义函数式接口，如果我们定义的接口不符合函数式的规范便会报错。配合Lambda表达式一起使用</p></blockquote><h4 id="四大核心函数式接口"><a href="#四大核心函数式接口" class="headerlink" title="四大核心函数式接口"></a>四大核心函数式接口</h4><table><thead><tr><th>函数式接口</th><th>参数类型</th><th>返回类型</th><th>用途</th></tr></thead><tbody><tr><td>Consumer<t>消费型接口</t></td><td>T</td><td>void</td><td>void accept(T t);</td></tr><tr><td>Supplier<t>供给型接口</t></td><td>void</td><td>T</td><td>T get();</td></tr><tr><td>Function&lt;T, R&gt; 函数型接口</td><td>T</td><td>R</td><td>R apply(T t);</td></tr><tr><td>Predicate<t>断言型接口</t></td><td>T</td><td>boolean</td><td>booelan test(T t);</td></tr><tr><td>BiConsumer&lt;T, U&gt;</td><td>T,U</td><td>void</td><td>对类型为T,U参数应用操作。包含方法为void accept(T t,U u);</td></tr><tr><td>BiFunction&lt;T, U, R&gt;</td><td>T,U</td><td>R</td><td>对类型为T,U参数应用操作，并返回R类型的结果。包含方法为R apply(T t,U u);</td></tr><tr><td>UnaryOperator<t>extends Function&lt;T, T&gt;</t></td><td>T</td><td>T</td><td>对类型为T的对象进行一元运算，并返回T类型的结果。包含方法为T apply(T t);</td></tr><tr><td>BinaryOperator<t>extends BiFunction&lt;T,T,T&gt;</t></td><td>T,T</td><td>T</td><td>对类型为T的对象进行二元运算，并返回T类型的结果。包含方法为T apply(T t1，T t2);</td></tr><tr><td>ToIntFunction<t>ToLongFunction<t>ToDoubleFunction<t></t></t></t></td><td>T</td><td>int long double</td><td>分别计算int、long、double值的函数</td></tr><tr><td>IntFunction<r>LongFunction<r>DoubleFunction<r></r></r></r></td><td>int long double</td><td>R</td><td>参数为int、long、double类型的函数</td></tr></tbody></table><blockquote><p>应用场景：当项目中需要一个接口，并且该接口中只有一个抽象方法，就没必要去创建新的接口，直接选择Java提供的使用合适的函数式接口即可</p></blockquote><h3 id="方法、构造方法和数组引用"><a href="#方法、构造方法和数组引用" class="headerlink" title="方法、构造方法和数组引用"></a>方法、构造方法和数组引用</h3><blockquote><p>方法、构造方法和数组引用就是Lambda的另一种表现形式</p></blockquote><h4 id="方法引用"><a href="#方法引用" class="headerlink" title="方法引用"></a>方法引用</h4><blockquote><p>若Lamdba表达式中的内容由方法已经实现了，可以使用方法引用这个技能</p><p>当你需要使用方法引用时，目标引用放在分隔符::前，方法的名称放在后面</p></blockquote><p><strong>对象::实例方法</strong></p><p>Lambda表达式中调用方法的参数类型和返回值必须和函数式接口中的抽象方法一致</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line"><span class="comment">//		I1 i1 = (x)-&gt;System.out.println(x);</span></span><br><span class="line"><span class="comment">//		i1.method(&quot;abcd&quot;);</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">//println里的参数列表和返回值类型必须和method方法一致才行</span></span><br><span class="line">		<span class="type">PrintStream</span> <span class="variable">ps</span> <span class="operator">=</span> System.out;</span><br><span class="line">		<span class="type">I1</span> <span class="variable">i1</span> <span class="operator">=</span> ps::println;<span class="comment">//对象::实例方法</span></span><br><span class="line">		i1.method(<span class="string">&quot;abcd&quot;</span>);	</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">I1</span>&#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">(String str)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong>类名::静态方法</strong></p><p>Lambda表达式中调用方法的参数类型和返回值必须和函数式接口中的抽象方法一致</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line"><span class="comment">//		Comparator&lt;Integer&gt; com = (x,y)-&gt; Integer.compare(x, y);</span></span><br><span class="line"><span class="comment">//		int compare = com.compare(10, 20);</span></span><br><span class="line"><span class="comment">//		System.out.println(compare);</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">//类名::静态方法</span></span><br><span class="line">		Comparator&lt;Integer&gt; com = Integer::compare;</span><br><span class="line">		<span class="type">int</span> <span class="variable">compare</span> <span class="operator">=</span> com.compare(<span class="number">10</span>, <span class="number">20</span>);</span><br><span class="line">		System.out.println(compare);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong>类名::实例方法</strong></p><p>Lambda表达式参数列表中第一个参数必须是实例方法的调用者</p><p>Lambda表达式参数列表中第二个参数必须是实例方法的参数</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line"><span class="comment">//		I1&lt;String&gt; i1 = (x,y) -&gt; x.equals(y);</span></span><br><span class="line"><span class="comment">//		boolean method = i1.method(&quot;abc&quot;, &quot;abc&quot;);</span></span><br><span class="line"><span class="comment">//		System.out.println(method);</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">//类名::实例方法</span></span><br><span class="line">		<span class="comment">//注意：Lambda表达式参数列表中第一个参数是equals方法的调用者，</span></span><br><span class="line">        <span class="comment">//	   Lambda表达式参数列表中第二个参数是equals方法的参数</span></span><br><span class="line">		I1&lt;String&gt; i1 = String::equals;</span><br><span class="line">		<span class="type">boolean</span> <span class="variable">method</span> <span class="operator">=</span> i1.method(<span class="string">&quot;abc&quot;</span>, <span class="string">&quot;abc&quot;</span>);</span><br><span class="line">		System.out.println(method);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">I1</span>&lt;T&gt;&#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">method</span><span class="params">(T t1,T t2)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="构造方法引用"><a href="#构造方法引用" class="headerlink" title="构造方法引用"></a>构造方法引用</h4><blockquote><p>类名::new</p><p>需要调用的构造方法的参数列表必须和函数式接口中抽象方法的参数列表一致</p></blockquote><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;	</span><br><span class="line">        <span class="comment">//需求：创建学生对象</span></span><br><span class="line">		I1&lt;Student&gt; i1 = Student::<span class="keyword">new</span>;</span><br><span class="line"><span class="comment">//		System.out.println(i1.method());</span></span><br><span class="line"><span class="comment">//      System.out.println(i1.method(&quot;桥本有菜&quot;,24));</span></span><br><span class="line">        System.out.println(i1.method(<span class="string">&quot;桥本有菜&quot;</span>,<span class="number">24</span>,<span class="number">8888</span>,Course.JAVA));</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">I1</span>&lt;T&gt;&#123;</span><br><span class="line"><span class="comment">//	public T method();</span></span><br><span class="line">	<span class="keyword">public</span> T <span class="title function_">method</span><span class="params">(String name,<span class="type">int</span> age,<span class="type">double</span> salary,Course course)</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">enum</span> <span class="title class_">Course</span>&#123;<span class="comment">//课程枚举</span></span><br><span class="line">	JAVA,HTML,PYTHON;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span>&#123;<span class="comment">//学生类</span></span><br><span class="line">	<span class="keyword">private</span> String name;</span><br><span class="line">	<span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line">	<span class="keyword">private</span> <span class="type">double</span> salary;</span><br><span class="line">	<span class="keyword">private</span> Course course;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="数组引用"><a href="#数组引用" class="headerlink" title="数组引用"></a>数组引用</h4><blockquote><p>语法格式：type[]::new</p></blockquote><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;	</span><br><span class="line">        <span class="comment">//创建数组</span></span><br><span class="line">		I1&lt;String[]&gt; i1 = String[]::<span class="keyword">new</span>;</span><br><span class="line">		System.out.println(Arrays.toString(i1.method(<span class="number">10</span>)));</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">I1</span>&lt;T&gt;&#123;</span><br><span class="line">	<span class="keyword">public</span> T <span class="title function_">method</span><span class="params">(<span class="type">int</span> capacity)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="Stream"><a href="#Stream" class="headerlink" title="Stream"></a>Stream</h3><h4 id="简介-2"><a href="#简介-2" class="headerlink" title="简介"></a>简介</h4><blockquote><p>Stream（流）是数据渠道，用于操作数据源（集合、数组等），生成元素序列。换言之，集合是存储数据的容器，流使用操作这些数据的</p><p>Stream可以对集合进行非常复杂的查找、过滤、映射数据等操作，类似于SQL执行数据库查询。Stream提供了一种高效且易于使用的处理数据的方式</p><p>注意：</p><ul><li>Stream不会存储数据</li><li>Stream不会改变源数据，通过一系列操作数据源会返回一个持有结果的新Stream</li><li>Stream操作是延迟执行的，意味着流会等到需要结果的时候才执行</li></ul></blockquote><h4 id="执行步骤"><a href="#执行步骤" class="headerlink" title="执行步骤"></a>执行步骤</h4><ol><li>创建Stream：通过数据源（集合、数组等）获取一个Stream</li><li>中间操作：中间操作链，对源数据的数据进行处理</li><li>终止操作：执行中间操作，并产生结果</li></ol><h4 id="创建Stream"><a href="#创建Stream" class="headerlink" title="创建Stream"></a>创建Stream</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="comment">//方式一：通过Collection接口提供的stream()-串行流或parallelStream()-并行流 获取流对象</span></span><br><span class="line">		List&lt;String&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">		Stream&lt;String&gt; stream1 = list.stream();</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//方式二：通过Arrays的静态方法stream()获取流对象</span></span><br><span class="line">		String[] strs = <span class="keyword">new</span> <span class="title class_">String</span>[<span class="number">10</span>];</span><br><span class="line">		Stream&lt;String&gt; stream2 = Arrays.stream(strs);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//方式三：通过Stream的静态方法of()获取流对象</span></span><br><span class="line">		Stream&lt;String&gt; stream3 = Stream.of(<span class="string">&quot;aaa&quot;</span>,<span class="string">&quot;bbb&quot;</span>,<span class="string">&quot;ccc&quot;</span>);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//方式四：创建无限流</span></span><br><span class="line">		<span class="comment">//iterate()迭代</span></span><br><span class="line">		Stream&lt;Integer&gt; stream4 = Stream.iterate(<span class="number">1</span>, (x)-&gt;x+=<span class="number">100</span>);</span><br><span class="line">		stream4.limit(<span class="number">3</span>).forEach(System.out::println);</span><br><span class="line">		<span class="comment">//生成</span></span><br><span class="line">		Stream&lt;Double&gt; stream5 = Stream.generate(()-&gt;Math.random());</span><br><span class="line">		stream5.limit(<span class="number">3</span>).forEach(System.out::println);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><blockquote><p>注意：多个中间操作可以连接成一个流水线，除非流水线触发终止操作，否则中间操作不会执行任何的处理，而在终止操作时一次性全部处理，称为惰性求值&#x2F;延迟加载</p></blockquote><h4 id="中间操作-筛选与切片"><a href="#中间操作-筛选与切片" class="headerlink" title="中间操作 - 筛选与切片"></a>中间操作 - 筛选与切片</h4><table><thead><tr><th>方法</th><th>描述</th></tr></thead><tbody><tr><td>filter(Predicate p)</td><td>从流中排除元素</td></tr><tr><td>limit(long maxSize)</td><td>设置限制数据条数</td></tr><tr><td>skip(long n)</td><td>跳过元素，返回跳过n个元素的流，若流中不满足n个元素则返回空流。与limit()互补</td></tr><tr><td>distinct()</td><td>筛选，流通过元素对象的hashCode()和equals()方法去除重复元素</td></tr></tbody></table><blockquote><p>如果没有终止操作，中间操作就不会被调用，终止操作时一次性全部处理，这种称为惰性求值&#x2F;延迟加载</p></blockquote><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	List&lt;Student&gt; stuList = Arrays.asList(</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;张三&quot;</span>, <span class="number">28</span>, <span class="number">4800</span>,Course.JAVA),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;李四&quot;</span>, <span class="number">36</span>, <span class="number">7200</span>,Course.JAVA),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;王五&quot;</span>, <span class="number">19</span>, <span class="number">9600</span>,Course.HTML),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;赵六&quot;</span>, <span class="number">42</span>, <span class="number">6100</span>,Course.HTML),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;孙七&quot;</span>, <span class="number">23</span>, <span class="number">9600</span>,Course.PYTHON),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;吴八&quot;</span>, <span class="number">31</span>, <span class="number">3000</span>,Course.PYTHON),</span><br><span class="line">   			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;李四&quot;</span>, <span class="number">36</span>, <span class="number">7200</span>,Course.JAVA));</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;	</span><br><span class="line">		<span class="comment">//需求1：过滤掉小于5000的学生对象</span></span><br><span class="line">		Stream&lt;Student&gt; stream = stuList.stream().filter((x)-&gt; &#123;</span><br><span class="line">			System.out.println(<span class="string">&quot;中间操作&quot;</span>);</span><br><span class="line">			<span class="keyword">return</span> x.getSalary()&gt;<span class="number">5000</span>;</span><br><span class="line">		&#125;);</span><br><span class="line">		<span class="comment">//迭代输出流里的数据就等同于终止操作</span></span><br><span class="line">		<span class="comment">//迭代功能在forEach()中完成，称为内部迭代（集合使用iterator()称为外部迭代）</span></span><br><span class="line">		stream.forEach(System.out::println);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test02</span><span class="params">()</span> &#123;	</span><br><span class="line">		<span class="comment">//需求2：过滤掉小于5000的学生对象，并显示3条</span></span><br><span class="line">		<span class="comment">//注意：因为限制了数据条数，所以满足数据条数后，后续的操作就不再运行了，效率就提高了</span></span><br><span class="line">		Stream&lt;Student&gt; stream = stuList.stream().filter((x)-&gt; &#123;</span><br><span class="line">			System.out.println(<span class="string">&quot;短路&quot;</span>);</span><br><span class="line">			<span class="keyword">return</span> x.getSalary()&gt;<span class="number">5000</span>;</span><br><span class="line">		&#125;).limit(<span class="number">3</span>);</span><br><span class="line">		<span class="comment">//迭代输出流里的数据就等同于终止操作</span></span><br><span class="line">		<span class="comment">//迭代功能在forEach()中完成，称为内部迭代（集合使用iterator()称为外部迭代）</span></span><br><span class="line">		stream.forEach(System.out::println);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test03</span><span class="params">()</span> &#123;	</span><br><span class="line">		<span class="comment">//需求3：过滤掉小于5000的学生对象，并跳过第1个学生对象</span></span><br><span class="line">		Stream&lt;Student&gt; stream = stuList.stream().</span><br><span class="line">				filter((x)-&gt; x.getSalary()&gt;<span class="number">5000</span>).</span><br><span class="line">				skip(<span class="number">1</span>);</span><br><span class="line">		<span class="comment">//迭代输出流里的数据就等同于终止操作</span></span><br><span class="line">		<span class="comment">//迭代功能在forEach()中完成，称为内部迭代（集合使用iterator()称为外部迭代）</span></span><br><span class="line">		stream.forEach(System.out::println);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test04</span><span class="params">()</span> &#123;	</span><br><span class="line">		<span class="comment">//需求4：过滤掉小于5000的学生对象，并筛选掉重复元素</span></span><br><span class="line">        <span class="comment">//Stream底层通过元素对象(Student对象)的hashCode()和equals()方法去除重复元素</span></span><br><span class="line">		Stream&lt;Student&gt; stream = stuList.stream().</span><br><span class="line">				filter((x)-&gt; x.getSalary()&gt;<span class="number">5000</span>).</span><br><span class="line">				distinct();</span><br><span class="line"></span><br><span class="line">		<span class="comment">//迭代输出流里的数据就等同于终止操作</span></span><br><span class="line">		<span class="comment">//迭代功能在forEach()中完成，称为内部迭代（集合使用iterator()称为外部迭代）</span></span><br><span class="line">		stream.forEach(System.out::println);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">enum</span> <span class="title class_">Course</span>&#123;<span class="comment">//课程枚举</span></span><br><span class="line">	JAVA,HTML,PYTHON;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span>&#123;<span class="comment">//学生类</span></span><br><span class="line">	<span class="keyword">private</span> String name;</span><br><span class="line">	<span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line">	<span class="keyword">private</span> <span class="type">double</span> salary;</span><br><span class="line">	<span class="keyword">private</span> Course course;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="中间操作-映射"><a href="#中间操作-映射" class="headerlink" title="中间操作 - 映射"></a>中间操作 - 映射</h4><table><thead><tr><th>方法</th><th>描述</th></tr></thead><tbody><tr><td>map(Function&lt;?, ? &gt; mapper)</td><td>将流中所有元素映射成一个新的元素或者提取信息</td></tr><tr><td>flatMap(Function&lt;?, ? extends Stream&lt;? &gt;&gt; mapper)</td><td>将流中的流整合（整合成平面&#x2F;平铺流）</td></tr></tbody></table><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line"></span><br><span class="line">	List&lt;String&gt; nameList = Arrays.asList(<span class="string">&quot;张三&quot;</span>,<span class="string">&quot;李四&quot;</span>,<span class="string">&quot;王五&quot;</span>,<span class="string">&quot;赵六&quot;</span>,<span class="string">&quot;孙七&quot;</span>,<span class="string">&quot;吴八&quot;</span>);	</span><br><span class="line">	List&lt;Student&gt; stuList = Arrays.asList(</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;张三&quot;</span>, <span class="number">28</span>, <span class="number">4800</span>,Course.JAVA),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;李四&quot;</span>, <span class="number">36</span>, <span class="number">7200</span>,Course.JAVA),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;王五&quot;</span>, <span class="number">19</span>, <span class="number">9600</span>,Course.HTML),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;赵六&quot;</span>, <span class="number">42</span>, <span class="number">6100</span>,Course.HTML),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;孙七&quot;</span>, <span class="number">23</span>, <span class="number">9600</span>,Course.PYTHON),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;吴八&quot;</span>, <span class="number">31</span>, <span class="number">3000</span>,Course.PYTHON),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;李四&quot;</span>, <span class="number">36</span>, <span class="number">7200</span>,Course.JAVA));</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;	</span><br><span class="line">		<span class="comment">//map() - 将流中所有元素映射成一个新的元素 或者 提取信息</span></span><br><span class="line">        </span><br><span class="line">		<span class="comment">//方式1：映射成一个新的元素</span></span><br><span class="line">        <span class="comment">//需求：//需求：nameList获取流对象，打印出所有学生的姓氏</span></span><br><span class="line">		nameList.stream().map((str)-&gt; str.charAt(<span class="number">0</span>)).forEach(System.out::println);</span><br><span class="line">		</span><br><span class="line">        <span class="comment">//方式2：映射成提取信息</span></span><br><span class="line">        <span class="comment">//需求：把原来流中的学生对象替换成学生姓名</span></span><br><span class="line">		stuList.stream().map((stu)-&gt; stu.getName()).forEach(System.out::println);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test02</span><span class="params">()</span> &#123;	</span><br><span class="line">		<span class="comment">//带着需求学flatMap()</span></span><br><span class="line">		<span class="comment">//flatMap() - 将流中的流整合（整合成平面/平铺流）</span></span><br><span class="line">		<span class="comment">//需求：将nameList里的字符串转换为字符输出</span></span><br><span class="line"></span><br><span class="line">		<span class="comment">//解决方案1：使用map()完成需求，可以看到流里包含另外的流，非常麻烦</span></span><br><span class="line">		Stream&lt;Stream&lt;Character&gt;&gt; stream = nameList.stream().</span><br><span class="line">				map(Test1::getCharacterStream);<span class="comment">//&#123;&#123;&#x27;张&#x27;,&#x27;三&#x27;&#125;,&#123;&#x27;李&#x27;,&#x27;四&#x27;&#125;,...&#125;</span></span><br><span class="line">		stream.forEach((sm) -&gt; &#123;</span><br><span class="line">			sm.forEach(System.out::println);</span><br><span class="line">		&#125;);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//解决方案2：使用flatMap()完成需求，将流中的流一并整合</span></span><br><span class="line">		nameList.stream().flatMap((str)-&gt; getCharacterStream(str)).</span><br><span class="line">		forEach(System.out::println);<span class="comment">//&#123;&#x27;张&#x27;,&#x27;三&#x27;&#125;,&#123;&#x27;李&#x27;,&#x27;四&#x27;&#125;,...</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//将字符串拆分出字符转换为流的方法</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> Stream&lt;Character&gt; <span class="title function_">getCharacterStream</span><span class="params">(String str)</span>&#123;</span><br><span class="line">		ArrayList&lt;Character&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line"></span><br><span class="line">		<span class="keyword">for</span> (Character c : str.toCharArray()) &#123;</span><br><span class="line">			list.add(c);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> list.stream();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">enum</span> <span class="title class_">Course</span>&#123;<span class="comment">//课程枚举</span></span><br><span class="line">	JAVA,HTML,PYTHON;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span>&#123;<span class="comment">//学生类</span></span><br><span class="line">	<span class="keyword">private</span> String name;</span><br><span class="line">	<span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line">	<span class="keyword">private</span> <span class="type">double</span> salary;</span><br><span class="line">	<span class="keyword">private</span> Course course;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="中间操作-排序"><a href="#中间操作-排序" class="headerlink" title="中间操作 - 排序"></a>中间操作 - 排序</h4><table><thead><tr><th>方法</th><th>解释</th></tr></thead><tbody><tr><td>sorted()</td><td>使用元素原有排序规则 - Comparable<t></t></td></tr><tr><td>sorted(Comparator&lt;? super T&gt; comparator)</td><td>使用自定义排序规则 - Comparator<t></t></td></tr></tbody></table><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	List&lt;Student&gt; stuList = Arrays.asList(</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;张三&quot;</span>, <span class="number">28</span>, <span class="number">4800</span>,Course.JAVA),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;李四&quot;</span>, <span class="number">36</span>, <span class="number">7200</span>,Course.JAVA),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;王五&quot;</span>, <span class="number">19</span>, <span class="number">9600</span>,Course.HTML),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;赵六&quot;</span>, <span class="number">42</span>, <span class="number">6100</span>,Course.HTML),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;孙七&quot;</span>, <span class="number">23</span>, <span class="number">9600</span>,Course.PYTHON),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;吴八&quot;</span>, <span class="number">31</span>, <span class="number">3000</span>,Course.PYTHON),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;吴八&quot;</span>, <span class="number">31</span>, <span class="number">3000</span>,Course.PYTHON));</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;	</span><br><span class="line">        <span class="comment">//使用元素原有排序规则（Comparable&lt;T&gt;）</span></span><br><span class="line">        <span class="comment">//需求：按照年龄排序</span></span><br><span class="line">		stuList.stream().sorted().forEach(System.out::println);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test02</span><span class="params">()</span> &#123;	</span><br><span class="line">		<span class="comment">//使用自定义排序规则（Comparator&lt;T&gt;）</span></span><br><span class="line">        <span class="comment">//需求：按照工资排序</span></span><br><span class="line">		stuList.stream().sorted((e1,e2)-&gt;&#123;</span><br><span class="line">			<span class="keyword">if</span>(e1.getSalary() == e2.getSalary())&#123;</span><br><span class="line">				<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">return</span> (<span class="type">int</span>)(e1.getSalary() - e2.getSalary());</span><br><span class="line">		&#125;).forEach(System.out::println);	</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">enum</span> <span class="title class_">Course</span>&#123;<span class="comment">//课程枚举</span></span><br><span class="line">	JAVA,HTML,PYTHON;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span> <span class="keyword">implements</span> <span class="title class_">Comparable</span>&lt;Student&gt;&#123;<span class="comment">//学生类</span></span><br><span class="line">	<span class="keyword">private</span> String name;</span><br><span class="line">	<span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line">	<span class="keyword">private</span> <span class="type">double</span> salary;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="终止操作-匹配与查找"><a href="#终止操作-匹配与查找" class="headerlink" title="终止操作 - 匹配与查找"></a>终止操作 - 匹配与查找</h4><table><thead><tr><th>方法</th><th>描述</th></tr></thead><tbody><tr><td>allMatch(Predicate&lt;? super T&gt; predicate)</td><td>检查是否匹配所有元素</td></tr><tr><td>anyMatch(Predicate&lt;? super T&gt; predicate)</td><td>检查是否匹配至少一个元素</td></tr><tr><td>noneMatch(Predicate&lt;? super T&gt; predicate)</td><td>检查是否没有匹配所有元素</td></tr><tr><td>findFirst()</td><td>返回第一个元素</td></tr><tr><td>findAny()</td><td>返回任意一个元素（但效果不好）</td></tr><tr><td>count()</td><td>返回流中元素的个数</td></tr><tr><td>max(Comparator&lt;? super T&gt; comparator)</td><td>返回流中最大值</td></tr><tr><td>min(Comparator&lt;? super T&gt; comparator)</td><td>返回流中最小值</td></tr></tbody></table><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	List&lt;Student&gt; stuList = Arrays.asList(</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;张三&quot;</span>, <span class="number">28</span>, <span class="number">4800</span>,Course.JAVA),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;李四&quot;</span>, <span class="number">36</span>, <span class="number">7200</span>,Course.JAVA),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;王五&quot;</span>, <span class="number">19</span>, <span class="number">9600</span>,Course.HTML),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;赵六&quot;</span>, <span class="number">42</span>, <span class="number">6100</span>,Course.HTML),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;孙七&quot;</span>, <span class="number">23</span>, <span class="number">9600</span>,Course.PYTHON),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;吴八&quot;</span>, <span class="number">31</span>, <span class="number">3000</span>,Course.PYTHON),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;吴八&quot;</span>, <span class="number">31</span>, <span class="number">3000</span>,Course.PYTHON));</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;	</span><br><span class="line">		<span class="comment">//需求1：检查流中素所有元素是否匹配 工资&gt;5000</span></span><br><span class="line">		<span class="type">boolean</span> <span class="variable">allMatch</span> <span class="operator">=</span> stuList.stream().allMatch((stu) -&gt; stu.getSalary()&gt;<span class="number">5000</span>);</span><br><span class="line">		System.out.println(allMatch);</span><br><span class="line">	</span><br><span class="line">		<span class="comment">//需求2：检查流中素所有元素至少匹配一个 工资&gt;5000</span></span><br><span class="line">		<span class="type">boolean</span>  <span class="variable">anyMatch</span> <span class="operator">=</span> stuList.stream().anyMatch((stu) -&gt; stu.getSalary()&gt;<span class="number">5000</span>);</span><br><span class="line">		System.out.println(anyMatch);</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//需求3：检查流中素所有元素是否没有匹配 工资&gt;5000</span></span><br><span class="line">		<span class="type">boolean</span> <span class="variable">noneMatch</span> <span class="operator">=</span> stuList.stream().noneMatch((stu) -&gt; 				</span><br><span class="line">                stu.getSalary()&gt;<span class="number">5000</span>);</span><br><span class="line">		System.out.println(noneMatch);</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//需求3：返回工资最高的学生信息</span></span><br><span class="line">		Optional&lt;Student&gt; findFirst = stuList.stream().</span><br><span class="line">				sorted((stu1,stu2)-&gt;Double.compare(</span><br><span class="line">                    stu1.getSalary(),stu2.getSalary())).</span><br><span class="line">				findFirst();</span><br><span class="line">		<span class="type">Student</span> <span class="variable">stu</span> <span class="operator">=</span> findFirst.get();</span><br><span class="line">		<span class="comment">//这种写法防止NullPointerException出现</span></span><br><span class="line">		<span class="comment">//Student stu = findFirst.orElse(new Student());</span></span><br><span class="line">		System.out.println(stu);</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//需求4：返回随机学生信息（但效果不好）</span></span><br><span class="line">		Optional&lt;Student&gt; findAny = stuList.stream().findAny();</span><br><span class="line">		<span class="type">Student</span> <span class="variable">stu</span> <span class="operator">=</span> findAny.get();</span><br><span class="line">		System.out.println(stu);</span><br><span class="line">        </span><br><span class="line">       <span class="comment">//需求5：获取学生个数</span></span><br><span class="line">		<span class="type">long</span> <span class="variable">count</span> <span class="operator">=</span> stuList.stream().count();</span><br><span class="line">		System.out.println(count);</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//需求6：获取最高工资的学生信息</span></span><br><span class="line">		Optional&lt;Student&gt; max = stuList.stream().</span><br><span class="line">				max((stu1,stu2)-&gt;Double.compare(stu1.getSalary(),stu2.getSalary()));</span><br><span class="line">		System.out.println(max.get());</span><br><span class="line">        </span><br><span class="line">       <span class="comment">//需求7：获取最工资的学生信息</span></span><br><span class="line">		Optional&lt;Student&gt; min = stuList.stream().</span><br><span class="line">				min((stu1,stu2)-&gt;Double.compare(stu1.getSalary(),stu2.getSalary()));</span><br><span class="line">		System.out.println(min.get());</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">enum</span> <span class="title class_">Course</span>&#123;<span class="comment">//课程枚举</span></span><br><span class="line">	JAVA,HTML,PYTHON;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span> <span class="keyword">implements</span> <span class="title class_">Comparable</span>&lt;Student&gt;&#123;<span class="comment">//学生类</span></span><br><span class="line">	<span class="keyword">private</span> String name;</span><br><span class="line">	<span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line">	<span class="keyword">private</span> <span class="type">double</span> salary;</span><br><span class="line">	<span class="keyword">private</span> Course course;</span><br><span class="line">   	...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="终止操作-归约"><a href="#终止操作-归约" class="headerlink" title="终止操作 - 归约"></a>终止操作 - 归约</h4><blockquote><p>归约：将流中的元素反复结合起来，得到一个值</p><p>map+reduce的连接通常称为map_reduce模式，因Google用它进行网络搜索而出名</p></blockquote><table><thead><tr><th>方法</th><th>描述</th></tr></thead><tbody><tr><td>reduce( T identity , BinaryOperator<t>accumulator)</t></td><td>参数：(初始值，结合逻辑)</td></tr><tr><td>reduce(BinaryOperator<t>accumulator)</t></td><td>参数：(结合逻辑)</td></tr></tbody></table><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line"></span><br><span class="line">	List&lt;Integer&gt; numList = Arrays.asList(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>,<span class="number">10</span>);</span><br><span class="line">	List&lt;Student&gt; stuList = Arrays.asList(</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;张三&quot;</span>, <span class="number">28</span>, <span class="number">4800</span>,Course.JAVA),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;李四&quot;</span>, <span class="number">36</span>, <span class="number">7200</span>,Course.JAVA),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;王五&quot;</span>, <span class="number">19</span>, <span class="number">9600</span>,Course.HTML),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;赵六&quot;</span>, <span class="number">42</span>, <span class="number">6100</span>,Course.HTML),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;孙七&quot;</span>, <span class="number">23</span>, <span class="number">9600</span>,Course.PYTHON),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;吴八&quot;</span>, <span class="number">31</span>, <span class="number">3000</span>,Course.PYTHON),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;李四&quot;</span>, <span class="number">36</span>, <span class="number">7200</span>,Course.JAVA));</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;	</span><br><span class="line">		<span class="comment">//需求：获取numList集合中元素的总和</span></span><br><span class="line">		<span class="type">Integer</span> <span class="variable">reduce</span> <span class="operator">=</span> numList.stream().</span><br><span class="line">				reduce(<span class="number">0</span>, (x,y)-&gt;x+y);</span><br><span class="line">		System.out.println(reduce);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test02</span><span class="params">()</span> &#123;	</span><br><span class="line">		<span class="comment">//需求：获取stuList集合中所有学生工资总和</span></span><br><span class="line">		Optional&lt;Double&gt; reduce = stuList.stream().</span><br><span class="line">				map(Student::getSalary).reduce(Double::sum);</span><br><span class="line">		<span class="type">Double</span> <span class="variable">sumSalary</span> <span class="operator">=</span> reduce.get();</span><br><span class="line">		System.out.println(sumSalary);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">enum</span> <span class="title class_">Course</span>&#123;<span class="comment">//课程枚举</span></span><br><span class="line">	JAVA,HTML,PYTHON;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span> <span class="keyword">implements</span> <span class="title class_">Comparable</span>&lt;Student&gt;&#123;<span class="comment">//学生类</span></span><br><span class="line">	<span class="keyword">private</span> String name;</span><br><span class="line">	<span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line">	<span class="keyword">private</span> <span class="type">double</span> salary;</span><br><span class="line">	<span class="keyword">private</span> Course course;</span><br></pre></td></tr></table></figure><h4 id="终止操作-收集"><a href="#终止操作-收集" class="headerlink" title="终止操作 - 收集"></a>终止操作 - 收集</h4><blockquote><p>收集：将流转换为其他形式。接收一个Collector接口的实现，用于给Stream中元素做汇总的方法</p></blockquote><table><thead><tr><th>方法</th><th>描述</th></tr></thead><tbody><tr><td>collect(Collector&lt;? super T, A, R&gt; collector)</td><td>把元素放入Collector集合中</td></tr></tbody></table><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line"></span><br><span class="line">	List&lt;Student&gt; stuList = Arrays.asList(</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;张三&quot;</span>, <span class="number">28</span>, <span class="number">4800</span>,Course.JAVA),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;李四&quot;</span>, <span class="number">36</span>, <span class="number">7200</span>,Course.JAVA),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;王五&quot;</span>, <span class="number">19</span>, <span class="number">9600</span>,Course.HTML),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;赵六&quot;</span>, <span class="number">42</span>, <span class="number">6100</span>,Course.HTML),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;孙七&quot;</span>, <span class="number">23</span>, <span class="number">9600</span>,Course.PYTHON),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;吴八&quot;</span>, <span class="number">31</span>, <span class="number">3000</span>,Course.PYTHON),</span><br><span class="line">			<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;李四&quot;</span>, <span class="number">36</span>, <span class="number">7200</span>,Course.JAVA));</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="comment">//把数据收集到集合中</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">//需求1：把当前学生姓名提取出来，并把数据放入List集合中</span></span><br><span class="line">		List&lt;String&gt; list = stuList.stream().</span><br><span class="line">				map(Student::getName).</span><br><span class="line">				collect(Collectors.toList());</span><br><span class="line">		list.forEach(System.out::println);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//需求2：把当前学生姓名提取出来，并把数据放入Set集合中</span></span><br><span class="line">		Set&lt;String&gt; set = stuList.stream().</span><br><span class="line">				map(Student::getName).collect(Collectors.toSet());</span><br><span class="line">		set.forEach(System.out::println);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//需求3：把当前学生姓名提取出来，并把数据放入指定集合中</span></span><br><span class="line">		HashSet&lt;String&gt; hashSet = stuList.stream().</span><br><span class="line">			map(Student::getName).collect(Collectors.toCollection(HashSet::<span class="keyword">new</span>));</span><br><span class="line">		hashSet.forEach(System.out::println);</span><br><span class="line">	&#125;	</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test02</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="comment">//收集流中的各种数据</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">//需求1：收集/获取学生个数</span></span><br><span class="line">		<span class="type">Long</span> <span class="variable">count</span> <span class="operator">=</span> stuList.stream().</span><br><span class="line">				map(Student::getName).collect(Collectors.counting());</span><br><span class="line">		System.out.println(count);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//需求2：收集/获取学生平均工资</span></span><br><span class="line">		<span class="type">Double</span> <span class="variable">avg</span> <span class="operator">=</span> stuList.stream().</span><br><span class="line">				collect(Collectors.averagingDouble(Student::getSalary));</span><br><span class="line">		System.out.println(avg);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//需求3：收集/获取学生总工资</span></span><br><span class="line">		<span class="type">Double</span> <span class="variable">sum</span> <span class="operator">=</span> stuList.stream().</span><br><span class="line">				collect(Collectors.summingDouble(Student::getSalary));</span><br><span class="line">		System.out.println(sum);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//需求4：收集/获取学生工资最大值</span></span><br><span class="line">		Optional&lt;Double&gt; max = stuList.stream().map(Student::getSalary).</span><br><span class="line">				collect(Collectors.maxBy(Double::compareTo));</span><br><span class="line">		System.out.println(max.get());</span><br><span class="line"></span><br><span class="line">		<span class="comment">//需求5：收集/获取学生工资最小值</span></span><br><span class="line">		Optional&lt;Double&gt; min = stuList.stream().map(Student::getSalary).</span><br><span class="line">				collect(Collectors.minBy(Double::compareTo));</span><br><span class="line">		System.out.println(min.get());</span><br><span class="line"></span><br><span class="line">		<span class="comment">//需求6：收集/获取工资最多的学生信息</span></span><br><span class="line">		Optional&lt;Student&gt; maxStu = stuList.stream().</span><br><span class="line">				collect(Collectors.maxBy(</span><br><span class="line">						(stu1,stu2)-&gt; (<span class="type">int</span>)(stu1.getSalary()-stu2.getSalary())));</span><br><span class="line">		System.out.println(maxStu.get());</span><br><span class="line"></span><br><span class="line">		<span class="comment">//需求7：收集/获取工资最多的学生信息</span></span><br><span class="line">		Optional&lt;Student&gt; minStu = stuList.stream().</span><br><span class="line">				collect(Collectors.maxBy(</span><br><span class="line">                    (stu1,stu2)-&gt; -(<span class="type">int</span>)(stu1.getSalary()-stu2.getSalary())));</span><br><span class="line">		System.out.println(minStu.get());</span><br><span class="line">	&#125;	</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test03</span><span class="params">()</span> &#123;<span class="comment">//分组</span></span><br><span class="line">		<span class="comment">//需求：按照学科分组</span></span><br><span class="line">		Map&lt;Course, List&lt;Student&gt;&gt; map = stuList.stream().collect(</span><br><span class="line">            Collectors.groupingBy(Student::getCourse));</span><br><span class="line">		System.out.println(map);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test04</span><span class="params">()</span> &#123;<span class="comment">//多级分组</span></span><br><span class="line">		<span class="comment">//需求：按照学科分组，在按照年龄分组</span></span><br><span class="line">		Map&lt;Course, Map&lt;String, List&lt;Student&gt;&gt;&gt; map = stuList.stream().</span><br><span class="line">				collect(Collectors.groupingBy(</span><br><span class="line">						Student::getCourse,Collectors.groupingBy((stu)-&gt;&#123;</span><br><span class="line">							<span class="keyword">if</span>(((Student)stu).getAge() &lt; <span class="number">28</span>)&#123;</span><br><span class="line">								<span class="keyword">return</span> <span class="string">&quot;青年&quot;</span>;</span><br><span class="line">							&#125;<span class="keyword">else</span> <span class="keyword">if</span>(((Student)stu).getAge() &lt; <span class="number">40</span>)&#123;</span><br><span class="line">								<span class="keyword">return</span> <span class="string">&quot;中年&quot;</span>;</span><br><span class="line">							&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">								<span class="keyword">return</span> <span class="string">&quot;老年&quot;</span>;</span><br><span class="line">							&#125;</span><br><span class="line">						&#125;)));</span><br><span class="line">		System.out.println(map);</span><br><span class="line">	&#125;</span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test05</span><span class="params">()</span> &#123;<span class="comment">//分区</span></span><br><span class="line">        <span class="comment">//需求：按照工资5000为标准分区</span></span><br><span class="line">		Map&lt;Boolean, List&lt;Student&gt;&gt; map = stuList.stream().collect(</span><br><span class="line">				Collectors.partitioningBy((stu) -&gt; stu.getSalary()&gt;<span class="number">5000</span>));</span><br><span class="line">		System.out.println(map);</span><br><span class="line">	&#125;</span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test06</span><span class="params">()</span> &#123;<span class="comment">//获取元素中字段的各种信息</span></span><br><span class="line">		<span class="comment">//需求：获取学生工资信息，再获取总值、平均值、最大值、最小值</span></span><br><span class="line">		<span class="type">DoubleSummaryStatistics</span> <span class="variable">collect</span> <span class="operator">=</span> stuList.stream().collect(</span><br><span class="line">				Collectors.summarizingDouble(Student::getSalary));</span><br><span class="line">		System.out.println(collect.getSum());</span><br><span class="line">		System.out.println(collect.getAverage());	</span><br><span class="line">		System.out.println(collect.getMax());</span><br><span class="line">		System.out.println(collect.getMin());</span><br><span class="line">	&#125;</span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test07</span><span class="params">()</span> &#123;<span class="comment">//拼接信息</span></span><br><span class="line">		<span class="comment">//需求：拼接学生姓名</span></span><br><span class="line">		<span class="type">String</span> <span class="variable">str1</span> <span class="operator">=</span> stuList.stream().map(Student::getName).collect(</span><br><span class="line">				Collectors.joining());</span><br><span class="line">		System.out.println(str1);</span><br><span class="line">		</span><br><span class="line">		<span class="type">String</span> <span class="variable">str2</span> <span class="operator">=</span> stuList.stream().map(Student::getName).collect(</span><br><span class="line">				Collectors.joining(<span class="string">&quot;,&quot;</span>));</span><br><span class="line">		System.out.println(str2);</span><br><span class="line">		</span><br><span class="line">		<span class="type">String</span> <span class="variable">str3</span> <span class="operator">=</span> stuList.stream().map(Student::getName).collect(</span><br><span class="line">				Collectors.joining(<span class="string">&quot;,&quot;</span>,<span class="string">&quot;--&quot;</span>,<span class="string">&quot;--&quot;</span>));</span><br><span class="line">		System.out.println(str3);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">enum</span> <span class="title class_">Course</span>&#123;<span class="comment">//课程枚举</span></span><br><span class="line">	JAVA,HTML,PYTHON;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span> <span class="keyword">implements</span> <span class="title class_">Comparable</span>&lt;Student&gt;&#123;<span class="comment">//学生类</span></span><br><span class="line">	<span class="keyword">private</span> String name;</span><br><span class="line">	<span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line">	<span class="keyword">private</span> <span class="type">double</span> salary;</span><br><span class="line">	<span class="keyword">private</span> Course course;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="并行流与串行流"><a href="#并行流与串行流" class="headerlink" title="并行流与串行流"></a>并行流与串行流</h3><blockquote><p>并行流就是把一个内容拆分成多个数据块，并用不同的线程分别处理每个数据块的流。Java8中将并行进行了优化，我们可以很容易的对数据进行并行操作。Stream API可以声明性地通过 parallel() - 并行流 与sequential()-顺序流 之间进行切换。</p><p>注意</p><ol><li>默认为顺序流&#x2F;串行流</li><li>并行流一般在大数据搜索里使用到</li><li>JDK1.8之前也有并行流，叫做Fork&#x2F;Join并行计算框架</li></ol></blockquote><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="comment">//需求：使用并行流计算1-10000000L之和</span></span><br><span class="line">		<span class="type">OptionalLong</span> <span class="variable">reduce</span> <span class="operator">=</span> LongStream.</span><br><span class="line">            range(<span class="number">0</span>, <span class="number">10000001L</span>).<span class="comment">//生成1-10000000的数流</span></span><br><span class="line">            parallel().			<span class="comment">//转换为并行流</span></span><br><span class="line">            reduce(Long::sum);	<span class="comment">//求和</span></span><br><span class="line">		System.out.println(reduce);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="Optional"><a href="#Optional" class="headerlink" title="Optional"></a>Optional</h3><blockquote><p>Optional<t>类(java. util. Optional)是一个容器类，代表一个存在或不存在的值，原来用null表示一个值不</t></p><p>存在，现在Optional可以更好的表达这个概念。并且可以避免空指针异常</p><p>此类的设计就是更好的避免空指针异常</p></blockquote><table><thead><tr><th>方法</th><th>描述</th></tr></thead><tbody><tr><td>Optional.of(T t)</td><td>创建一个Optional实例</td></tr><tr><td>Optional.empty()</td><td>创建一 个空的 Optional实例</td></tr><tr><td>Optional.ofNullable(T t)</td><td>若t不为null,创建Optional实例,否则创建空实例</td></tr><tr><td>get()</td><td>获取Optional实例里的对象</td></tr><tr><td>isPresent()</td><td>判断是否包含值</td></tr><tr><td>orElse(T t)</td><td>如果调用对象包含值, 返回该值，否则返回t</td></tr><tr><td>orElseGet(Supplier s)</td><td>如果调用对象包含值，返回该值,否则返回s获取的值</td></tr><tr><td>map(Function f)</td><td>如果有值对其处理，并返回处理后的Optional,否则返回optional. empty()</td></tr><tr><td>flatMap(Function mapper)</td><td>与map 类似，要求返回值必须是Optional</td></tr></tbody></table><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="comment">//创建一个Optional实例，把对象封装到Optional容器里</span></span><br><span class="line"><span class="comment">//		Optional&lt;Student&gt; op = Optional.of(</span></span><br><span class="line"><span class="comment">//          new Student(&quot;aaa&quot;, 26, 6666, Course.HTML));</span></span><br><span class="line">		<span class="comment">//创建一个空的Optional容器</span></span><br><span class="line"><span class="comment">//		Optional&lt;Student&gt; op = Optional.empty();</span></span><br><span class="line">		<span class="comment">//创建一个Optional实例，若对象为null-&gt;创建空实例，若对象为不为null-&gt;创建Optional实例</span></span><br><span class="line">		Optional&lt;Student&gt; op = Optional.ofNullable(</span><br><span class="line">            <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;bbb&quot;</span>, <span class="number">26</span>, <span class="number">7777</span>, Course.PYTHON));</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//判断容器里是否包含对象</span></span><br><span class="line">		<span class="keyword">if</span>(op.isPresent())&#123;<span class="comment">//包含</span></span><br><span class="line">			System.out.println(<span class="string">&quot;获取容器里的对象：&quot;</span> + op.get().getName());</span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;<span class="comment">//不包含</span></span><br><span class="line">			System.out.println(<span class="string">&quot;容器里的对象为null&quot;</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//如果容器里有对象就返回，否则就返回新对象</span></span><br><span class="line">		<span class="type">Student</span> <span class="variable">stu</span> <span class="operator">=</span> op.orElse(<span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;ccc&quot;</span>, <span class="number">26</span>, <span class="number">8888</span>, Course.JAVA));</span><br><span class="line">		System.out.println(stu.getName());</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//不同情况下可以返回不同对象，orElseGet()比orElse()可变性更强</span></span><br><span class="line">		<span class="type">boolean</span> <span class="variable">bool</span> <span class="operator">=</span> <span class="literal">true</span>;</span><br><span class="line">		stu = op.orElseGet(()-&gt;&#123;</span><br><span class="line">			<span class="keyword">if</span>(bool)&#123;</span><br><span class="line">				<span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;吴彦祖&quot;</span>, <span class="number">26</span>, <span class="number">8888</span>, Course.JAVA);</span><br><span class="line">			&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">				<span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;麻生希&quot;</span>, <span class="number">26</span>, <span class="number">8888</span>, Course.JAVA);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//获取原容器中的某个值并返回新容器中</span></span><br><span class="line">		<span class="comment">//map(Function&lt;? super T, ? extends U&gt; mapper)</span></span><br><span class="line">		Optional&lt;String&gt; map = op.map(Student::getName);</span><br><span class="line">		System.out.println(map.get());</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//与map 类似，要求返回值必须是Optional</span></span><br><span class="line">		<span class="comment">//flatMap(Function&lt;? super T, Optional&lt;U&gt;&gt; mapper)</span></span><br><span class="line">		Optional&lt;String&gt; flatMap = op.flatMap((e)-&gt;Optional.of(e.getName()));</span><br><span class="line">		System.out.println(flatMap.get());</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">enum</span> <span class="title class_">Course</span>&#123;<span class="comment">//课程枚举</span></span><br><span class="line">	JAVA,HTML,PYTHON;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span> <span class="keyword">implements</span> <span class="title class_">Comparable</span>&lt;Student&gt;&#123;<span class="comment">//学生类</span></span><br><span class="line">	<span class="keyword">private</span> String name;</span><br><span class="line">	<span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line">	<span class="keyword">private</span> <span class="type">double</span> salary;</span><br><span class="line">	<span class="keyword">private</span> Course course;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="接口的默认方法与静态方法"><a href="#接口的默认方法与静态方法" class="headerlink" title="接口的默认方法与静态方法"></a>接口的默认方法与静态方法</h3><blockquote><p>从JDK1.8开始，接口中可以有默认方法，既default修饰的方法，此方法可以让接口的实现类所调用，而接</p><p>口中的静态方法直接用接口名调用即可</p></blockquote><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="type">MyClass</span> <span class="variable">myClass</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">MyClass</span>();</span><br><span class="line">		myClass.defaultMethod();</span><br><span class="line">		</span><br><span class="line">		I1.staticMethod();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">I1</span>&#123;</span><br><span class="line">	<span class="keyword">default</span> <span class="keyword">void</span> <span class="title function_">defaultMethod</span><span class="params">()</span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;接口中的默认方法&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">staticMethod</span><span class="params">()</span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;接口中的静态方法&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MyClass</span> <span class="keyword">implements</span> <span class="title class_">I1</span>&#123;&#125;</span><br></pre></td></tr></table></figure><blockquote><p>接口默认方法的”类优先”原则：</p><ul><li><p>如果一个接口中定义了一个默认方法，而接口实现类的父类定义了一个同名的方法时，选择父类中的方法</p></li><li><p>接口冲突：如果一个父接口提供一个默认方法，而另一个接口也提供了一个具有相同名称和参数列表的方法(不管方法是否是默认方法)，那么必须覆盖该方法来解决冲突</p></li></ul></blockquote><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="type">MyClass</span> <span class="variable">myClass</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">MyClass</span>();</span><br><span class="line">		MyClass.method();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">I1</span>&#123;</span><br><span class="line">	<span class="keyword">default</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;I1接口中的默认方法&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Father</span> &#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;Father类中的默认方法&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MyClass</span> <span class="keyword">extends</span> <span class="title class_">Father</span> <span class="keyword">implements</span> <span class="title class_">I1</span> &#123;&#125;</span><br></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="type">MyClass</span> <span class="variable">myClass</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">MyClass</span>();</span><br><span class="line">		myClass.method();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">I1</span>&#123;</span><br><span class="line">	<span class="keyword">default</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;I1接口中的默认方法&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">I2</span>&#123;</span><br><span class="line">	<span class="keyword">default</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;I2接口中的默认方法&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MyClass</span> <span class="keyword">implements</span> <span class="title class_">I1</span>,I2 &#123;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="comment">//I1.super.method();</span></span><br><span class="line">		I2.<span class="built_in">super</span>.method();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="日期组件"><a href="#日期组件" class="headerlink" title="日期组件"></a>日期组件</h3><blockquote><p>JDK1.8提供的新日期类都是不可变的，既不管怎么样的改变，都会产生一个新的实例，他们都是线程安全的</p><p>日期组件遵循与IOS-8601世界时间标准</p></blockquote><h4 id="组件简介"><a href="#组件简介" class="headerlink" title="组件简介"></a>组件简介</h4><table><thead><tr><th>包路径</th><th>类名</th><th>描述</th></tr></thead><tbody><tr><td>java.time</td><td></td><td>针对日期和时间操作的包</td></tr><tr><td></td><td>LocalDate</td><td>用于表示日期的类</td></tr><tr><td></td><td>LocalTime</td><td>用于表示时间的类</td></tr><tr><td></td><td>LocalDateTime</td><td>用于表示日期时间的类</td></tr><tr><td></td><td>Instant</td><td>时间戳类（1970.1.1 0:0:0 到线程的毫秒数）</td></tr><tr><td></td><td>Period</td><td>两个日期间隔类</td></tr><tr><td></td><td>Duration</td><td>两个时间间隔类</td></tr><tr><td>java.time.chrono</td><td></td><td>针对日期时间特殊格式操作的包</td></tr><tr><td></td><td>JapaneseChronology</td><td>日本帝国历法系统类</td></tr><tr><td></td><td>ThaiBuddhistChronology</td><td>泰国佛教日历系统类</td></tr><tr><td>java.time.format</td><td></td><td>针对时间日期时间格式化操作的包</td></tr><tr><td></td><td>DateTimeFormatter</td><td>格式化日期时间类</td></tr><tr><td>java.time.temporal</td><td></td><td>针对时间矫正操作的包</td></tr><tr><td>java.time.zone</td><td></td><td>针对时区操作的包</td></tr></tbody></table><h4 id="日期时间类、时间戳、间隔类"><a href="#日期时间类、时间戳、间隔类" class="headerlink" title="日期时间类、时间戳、间隔类"></a>日期时间类、时间戳、间隔类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="comment">//LocalDate LocalTime LocalDateTime</span></span><br><span class="line">		<span class="comment">//这三个日期类的使用大致一样</span></span><br><span class="line"></span><br><span class="line">		<span class="comment">//获取当前日期时间对象</span></span><br><span class="line">		<span class="type">LocalDateTime</span> <span class="variable">ldt1</span> <span class="operator">=</span> LocalDateTime.now();</span><br><span class="line">		System.out.println(ldt1);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//获取指定日期时间对象</span></span><br><span class="line">		<span class="type">LocalDateTime</span> <span class="variable">ldt2</span> <span class="operator">=</span> LocalDateTime.of(<span class="number">2020</span>, <span class="number">1</span>, <span class="number">23</span>, <span class="number">8</span>, <span class="number">30</span>, <span class="number">10</span>, <span class="number">10</span>);</span><br><span class="line">		System.out.println(ldt2);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//获取ldt1推后的时间日期对象</span></span><br><span class="line">		<span class="type">LocalDateTime</span> <span class="variable">ldt3</span> <span class="operator">=</span> ldt1.plusYears(<span class="number">2</span>);</span><br><span class="line">		System.out.println(ldt3);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//获取ldt1提前的时间日期对象</span></span><br><span class="line">		<span class="type">LocalDateTime</span> <span class="variable">ldt4</span> <span class="operator">=</span> ldt3.minusMonths(<span class="number">2</span>);</span><br><span class="line"></span><br><span class="line">		System.out.println(ldt4.getYear());</span><br><span class="line">		System.out.println(ldt4.getMonthValue());</span><br><span class="line">		System.out.println(ldt4.getDayOfMonth());</span><br><span class="line">		System.out.println(ldt4.getHour());</span><br><span class="line">		System.out.println(ldt4.getMinute());</span><br><span class="line">		System.out.println(ldt4.getSecond());</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test02</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="comment">//使用时间戳（从1970年1月1日0:0:0到现在的毫秒值）</span></span><br><span class="line"></span><br><span class="line">		<span class="comment">//默认创建UTC(世界标准时间)时区的时间戳对象</span></span><br><span class="line">		<span class="type">Instant</span> <span class="variable">now1</span> <span class="operator">=</span> Instant.now();</span><br><span class="line">		System.out.println(now1);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//获取偏移8小时的偏移日期时间对象</span></span><br><span class="line">		<span class="type">OffsetDateTime</span> <span class="variable">odt</span> <span class="operator">=</span> now1.atOffset(ZoneOffset.ofHours(<span class="number">8</span>));</span><br><span class="line">		System.out.println(odt);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//获取时间戳的毫秒值形式</span></span><br><span class="line">		System.out.println(now1.toEpochMilli());</span><br><span class="line"></span><br><span class="line">		<span class="comment">//获取一个1970年1月1日0:0:0 往后退1秒的时间戳对象</span></span><br><span class="line">		<span class="type">Instant</span> <span class="variable">now2</span> <span class="operator">=</span> Instant.ofEpochSecond(<span class="number">1</span>);</span><br><span class="line">		System.out.println(now2);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test03</span><span class="params">()</span> <span class="keyword">throws</span> InterruptedException &#123;</span><br><span class="line">		<span class="comment">//Duration：时间间隔类</span></span><br><span class="line"></span><br><span class="line">		<span class="type">Instant</span> <span class="variable">now1</span> <span class="operator">=</span> Instant.now();</span><br><span class="line">		Thread.sleep(<span class="number">1000</span>);</span><br><span class="line">		<span class="type">Instant</span> <span class="variable">now2</span> <span class="operator">=</span> Instant.now();</span><br><span class="line">		<span class="comment">//获取时间间隔类对象</span></span><br><span class="line">		<span class="type">Duration</span> <span class="variable">duration1</span> <span class="operator">=</span> Duration.between(now1, now2);</span><br><span class="line">		System.out.println(duration1.toMillis());</span><br><span class="line">		</span><br><span class="line">		System.out.println(<span class="string">&quot;-----------------------------&quot;</span>);</span><br><span class="line">		</span><br><span class="line">		<span class="type">LocalTime</span> <span class="variable">lt1</span> <span class="operator">=</span> LocalTime.now();</span><br><span class="line">		Thread.sleep(<span class="number">1000</span>);</span><br><span class="line">		<span class="type">LocalTime</span> <span class="variable">lt2</span> <span class="operator">=</span> LocalTime.now();</span><br><span class="line">		<span class="comment">//获取时间间隔类对象</span></span><br><span class="line">		<span class="type">Duration</span> <span class="variable">duration2</span> <span class="operator">=</span> Duration.between(lt1, lt2);</span><br><span class="line">		System.out.println(duration2.toMillis());</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test04</span><span class="params">()</span> <span class="keyword">throws</span> InterruptedException &#123;</span><br><span class="line">		<span class="comment">//Period：日期间隔类</span></span><br><span class="line">        </span><br><span class="line">		<span class="type">LocalDate</span> <span class="variable">ld1</span> <span class="operator">=</span> LocalDate.now();</span><br><span class="line">		Thread.sleep(<span class="number">1000</span>);</span><br><span class="line">		<span class="type">LocalDate</span> <span class="variable">ld2</span> <span class="operator">=</span> LocalDate.of(<span class="number">2020</span>, <span class="number">12</span>, <span class="number">31</span>);</span><br><span class="line">		</span><br><span class="line">		<span class="type">Period</span> <span class="variable">period</span> <span class="operator">=</span> Period.between(ld1, ld2);</span><br><span class="line">		System.out.println(period.getYears());</span><br><span class="line">		System.out.println(period.getMonths());</span><br><span class="line">		System.out.println(period.getDays());</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="日期时间格式化类-DateTimeFormatter"><a href="#日期时间格式化类-DateTimeFormatter" class="headerlink" title="日期时间格式化类-DateTimeFormatter"></a>日期时间格式化类-DateTimeFormatter</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="comment">//格式化日期时间类</span></span><br><span class="line">		</span><br><span class="line">		<span class="type">LocalDateTime</span> <span class="variable">ldt1</span> <span class="operator">=</span> LocalDateTime.now();</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//获取本地标准的日期时间格式化对象</span></span><br><span class="line">		<span class="type">DateTimeFormatter</span> <span class="variable">dtf1</span> <span class="operator">=</span> DateTimeFormatter.ISO_LOCAL_DATE_TIME;</span><br><span class="line">		<span class="type">String</span> <span class="variable">strDateTime1</span> <span class="operator">=</span> ldt1.format(dtf1);<span class="comment">//格式化时间日期</span></span><br><span class="line">		System.out.println(strDateTime1);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//自定义日期时间格式化对象</span></span><br><span class="line">		<span class="type">DateTimeFormatter</span> <span class="variable">dtf2</span> <span class="operator">=</span> DateTimeFormatter.</span><br><span class="line">            ofPattern(<span class="string">&quot;yyyy年MM月dd日 HH:mm:ss&quot;</span>);</span><br><span class="line">		<span class="type">String</span> <span class="variable">strDateTime2</span> <span class="operator">=</span> ldt1.format(dtf2);<span class="comment">//格式化时间日期</span></span><br><span class="line">		System.out.println(strDateTime2);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//将指定格式的字符串解析成LocalDateTime对象</span></span><br><span class="line">		<span class="type">LocalDateTime</span> <span class="variable">parse</span> <span class="operator">=</span> LocalDateTime.parse(<span class="string">&quot;2020年03月12日 11:04:14&quot;</span>, dtf2);</span><br><span class="line">		System.out.println(parse);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="时间矫正器类-TemporalAdjuster"><a href="#时间矫正器类-TemporalAdjuster" class="headerlink" title="时间矫正器类-TemporalAdjuster"></a>时间矫正器类-TemporalAdjuster</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="comment">//时间矫正器</span></span><br><span class="line">		</span><br><span class="line">		<span class="type">LocalDateTime</span> <span class="variable">ldt1</span> <span class="operator">=</span> LocalDateTime.now();</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//设置指定月份</span></span><br><span class="line">		<span class="type">LocalDateTime</span> <span class="variable">ldt2</span> <span class="operator">=</span> ldt1.withMonth(<span class="number">10</span>);</span><br><span class="line">		System.out.println(ldt2);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//设置下一个周末</span></span><br><span class="line">		<span class="type">LocalDateTime</span> <span class="variable">ldt3</span> <span class="operator">=</span> ldt1.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));</span><br><span class="line">		System.out.println(ldt3);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//自定义时间矫正器：设置下一个工作</span></span><br><span class="line">		<span class="type">LocalDateTime</span> <span class="variable">ldt4</span> <span class="operator">=</span> ldt1.with((temporal)-&gt;&#123;</span><br><span class="line">			<span class="type">LocalDateTime</span> <span class="variable">ldt</span> <span class="operator">=</span> (LocalDateTime) temporal;</span><br><span class="line">			<span class="type">DayOfWeek</span> <span class="variable">week</span> <span class="operator">=</span> ldt.getDayOfWeek();</span><br><span class="line">			<span class="keyword">if</span>(week.equals(DayOfWeek.FRIDAY))&#123;<span class="comment">//周五</span></span><br><span class="line">				<span class="keyword">return</span> ldt.plusDays(<span class="number">3</span>);</span><br><span class="line">			&#125;<span class="keyword">else</span> <span class="keyword">if</span>(week.equals(DayOfWeek.SATURDAY))&#123;<span class="comment">//周六</span></span><br><span class="line">				<span class="keyword">return</span> ldt.plusDays(<span class="number">2</span>);</span><br><span class="line">			&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">				<span class="keyword">return</span> ldt.plusDays(<span class="number">1</span>);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;);</span><br><span class="line">		System.out.println(ldt4);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="时区类"><a href="#时区类" class="headerlink" title="时区类"></a>时区类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="comment">//时区类 </span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">//获取所有时区字符串</span></span><br><span class="line">		Set&lt;String&gt; set = ZoneId.getAvailableZoneIds();</span><br><span class="line">		<span class="keyword">for</span> (String str : set) &#123;</span><br><span class="line">			System.out.println(str);</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//获取指定时区的日期时间对象</span></span><br><span class="line">		<span class="type">LocalDateTime</span> <span class="variable">ldt1</span> <span class="operator">=</span> LocalDateTime.now(ZoneId.of(<span class="string">&quot;Asia/Tokyo&quot;</span>));</span><br><span class="line">		System.out.println(ldt1);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//获取指定时区的日期时间对象 + 偏移量</span></span><br><span class="line">		<span class="type">LocalDateTime</span> <span class="variable">ldt2</span> <span class="operator">=</span> LocalDateTime.now(ZoneId.of(<span class="string">&quot;Asia/Tokyo&quot;</span>));</span><br><span class="line">		<span class="type">ZonedDateTime</span> <span class="variable">zonedDateTime</span> <span class="operator">=</span> ldt2.atZone(ZoneId.of(<span class="string">&quot;Asia/Tokyo&quot;</span>));</span><br><span class="line">		System.out.println(zonedDateTime);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="重复注解及类型注解"><a href="#重复注解及类型注解" class="headerlink" title="重复注解及类型注解"></a>重复注解及类型注解</h3><blockquote><p>jdk1.8开始可以重复注解</p><p>ps：一个类可有多个同样的注解</p></blockquote><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="keyword">static</span> java.lang.annotation.ElementType.TYPE;</span><br><span class="line"><span class="keyword">import</span> <span class="keyword">static</span> java.lang.annotation.ElementType.PARAMETER;</span><br><span class="line"><span class="keyword">import</span> <span class="keyword">static</span> java.lang.annotation.ElementType.TYPE_PARAMETER;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.lang.annotation.Annotation;</span><br><span class="line"><span class="keyword">import</span> java.lang.annotation.Repeatable;</span><br><span class="line"><span class="keyword">import</span> java.lang.annotation.Retention;</span><br><span class="line"><span class="keyword">import</span> java.lang.annotation.RetentionPolicy;</span><br><span class="line"><span class="keyword">import</span> java.lang.annotation.Target;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Method;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"></span><br><span class="line"><span class="comment">//重复注解</span></span><br><span class="line"><span class="meta">@Author(name=&quot;何老师&quot;)</span></span><br><span class="line"><span class="meta">@Author(name=&quot;苍老师&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> <span class="keyword">throws</span> Exception&#123;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//需求1：获取Test1的多个作者注解</span></span><br><span class="line">		Class&lt;Test1&gt; ct = Test1.class;</span><br><span class="line">		Author[] as = ct.getAnnotationsByType(Author.class);</span><br><span class="line">		<span class="keyword">for</span> (Author a : as) &#123;</span><br><span class="line">			System.out.println(a.name());</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//需求2：获取method方法中参数里的注解</span></span><br><span class="line">		<span class="type">Method</span> <span class="variable">m</span> <span class="operator">=</span> ct.getMethod(<span class="string">&quot;method&quot;</span>, String.class);</span><br><span class="line">		Annotation[][] parameterAnnotations = m.getParameterAnnotations();</span><br><span class="line">		<span class="comment">//获取参数中注解列表（有可能一个参数多个注解）</span></span><br><span class="line">        <span class="keyword">for</span> (Annotation[] annotations : parameterAnnotations) &#123;</span><br><span class="line">			<span class="keyword">for</span> (Annotation an : annotations) &#123;<span class="comment">//获取具体注解</span></span><br><span class="line">				System.out.println(an);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">(<span class="meta">@Author(name=&quot;波老师&quot;)</span> String str)</span>&#123;&#125;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="comment">//作者注解的容器</span></span><br><span class="line"><span class="meta">@Target(TYPE)</span></span><br><span class="line"><span class="meta">@Retention(RetentionPolicy.RUNTIME)</span></span><br><span class="line"><span class="meta">@interface</span> Authors&#123;</span><br><span class="line">	Author[] value();</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//作者注解</span></span><br><span class="line"><span class="meta">@Repeatable(Authors.class)</span></span><br><span class="line"><span class="meta">@Target(&#123;TYPE, PARAMETER,TYPE_PARAMETER&#125;)</span><span class="comment">//TYPE_PARAMETER-类型注解：作用在参数</span></span><br><span class="line"><span class="meta">@Retention(RetentionPolicy.RUNTIME)</span></span><br><span class="line"><span class="meta">@interface</span> Author&#123;</span><br><span class="line">	String <span class="title function_">name</span><span class="params">()</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="copyright-cc-box"><i class="anzhiyufont anzhiyu-icon-copyright"></i></div><div class="post-copyright__author_box"><a class="post-copyright__author_img" target="_blank" rel="noopener" href="https://insectmk.cn" title="头像"><img class="post-copyright__author_img_back" src="/static/img/head/insectmk.jpg" title="头像" alt="头像"><img class="post-copyright__author_img_front" src="/static/img/head/insectmk.jpg" title="头像" alt="头像"></a><div class="post-copyright__author_name">InsectMk</div><div class="post-copyright__author_desc">今天特别开心！</div></div><div class="post-copyright__post__info"><a class="post-copyright__original" title="该文章为原创文章，注意版权协议" href="https://insectmk.top/posts/d111bf74/">原创</a><a class="post-copyright-title"><span onclick='rm.copyPageUrl("https://insectmk.top/posts/d111bf74/")'>JDK1.8新特性</span></a></div><div class="post-tools" id="post-tools"><div class="post-tools-left"><div class="rewardLeftButton"></div><div class="shareRight"><div class="share-link mobile"><div class="share-qrcode"><div class="share-button" title="使用手机访问这篇文章"><i class="anzhiyufont anzhiyu-icon-qrcode"></i></div><div class="share-main"><div class="share-main-all"><div id="qrcode" title="https://insectmk.top/posts/d111bf74/"></div><div class="reward-dec">使用手机访问这篇文章</div></div></div></div></div><div class="share-link weibo"><a class="share-button" target="_blank" href="https://service.weibo.com/share/share.php?title=undefined&amp;url=https://insectmk.top/posts/d111bf74/&amp;pic=undefined" rel="external nofollow noreferrer noopener"><i class="anzhiyufont anzhiyu-icon-weibo"></i></a></div><script>function copyCurrentPageUrl(){var e=window.location.href,t=document.createElement("input");t.setAttribute("value",e),document.body.appendChild(t),t.select(),t.setSelectionRange(0,99999),document.execCommand("copy"),document.body.removeChild(t)}</script><div class="share-link copyurl"><div class="share-button" id="post-share-url" title="复制链接" onclick="copyCurrentPageUrl()"><i class="anzhiyufont anzhiyu-icon-link"></i></div></div></div></div></div><div class="post-copyright__notice"><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://insectmk.top" target="_blank">InsectMk的个人空间</a>！</span></div></div><div class="post-tools-right"><div class="tag_share"><div class="post-meta__box"><div class="post-meta__box__tag-list"><a class="post-meta__box__tags" href="/tags/Java/"><span class="tags-punctuation"><i class="anzhiyufont anzhiyu-icon-tag"></i></span>Java<span class="tagsPageCount">35</span></a><a class="post-meta__box__tags" href="/tags/%E5%8D%83%E9%94%8B%E6%95%99%E8%82%B2/"><span class="tags-punctuation"> <i class="anzhiyufont anzhiyu-icon-tag"></i></span>千锋教育<span class="tagsPageCount">30</span></a><a class="post-meta__box__tags" href="/tags/%E5%9F%B9%E8%AE%AD/"><span class="tags-punctuation"> <i class="anzhiyufont anzhiyu-icon-tag"></i></span>培训<span class="tagsPageCount">30</span></a></div></div></div><div class="post_share"><div class="social-share" data-image="https://image.insectmk.cn/hexo-gitee-blog/cover/github-insectmk.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.cbd.int/butterfly-extsrc@1.1.3/sharejs/dist/css/share.min.css" media="print" onload='this.media="all"'><script src="https://cdn.cbd.int/butterfly-extsrc@1.1.3/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/a3ed9ac5/"><img class="prev-cover" src="/" onerror='onerror=null,src="/static/img/website/404.jpg"' alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Fork/Join并行计算框架</div></div></a></div><div class="next-post pull-right"><a href="/posts/de7c53fe/"><img class="next-cover" src="/" onerror='onerror=null,src="/static/img/website/404.jpg"' alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">线程池</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="anzhiyufont anzhiyu-icon-thumbs-up fa-fw" style="font-size:1.5rem;margin-right:4px"></i><span>喜欢这篇文章的人也看了</span></div><div class="relatedPosts-list"><div><a href="/posts/b51f2d85/" title="动&#x2F;静态语言、强&#x2F;弱类型语言、编译型&#x2F;解释形语言的区别"><img class="cover" src="/" alt="cover"><div class="content is-center"><div class="date"><i class="anzhiyufont anzhiyu-icon-calendar-days fa-fw"></i> 2023-06-26</div><div class="title">动&#x2F;静态语言、强&#x2F;弱类型语言、编译型&#x2F;解释形语言的区别</div></div></a></div><div><a href="/posts/836f8ca6/" title="千锋JavaDay01"><img class="cover" src="/" alt="cover"><div class="content is-center"><div class="date"><i class="anzhiyufont anzhiyu-icon-calendar-days fa-fw"></i> 2023-06-26</div><div class="title">千锋JavaDay01</div></div></a></div><div><a href="/posts/f9fe4650/" title="主方法的构造"><img class="cover" src="/" alt="cover"><div class="content is-center"><div class="date"><i class="anzhiyufont anzhiyu-icon-calendar-days fa-fw"></i> 2023-06-26</div><div class="title">主方法的构造</div></div></a></div><div><a href="/posts/1a66dd1c/" title="千锋JavaDay02"><img class="cover" src="/" alt="cover"><div class="content is-center"><div class="date"><i class="anzhiyufont anzhiyu-icon-calendar-days fa-fw"></i> 2023-06-26</div><div class="title">千锋JavaDay02</div></div></a></div><div><a href="/posts/d88f5f05/" title="字节码文件解析"><img class="cover" src="/" alt="cover"><div class="content is-center"><div class="date"><i class="anzhiyufont anzhiyu-icon-calendar-days fa-fw"></i> 2023-06-26</div><div class="title">字节码文件解析</div></div></a></div><div><a href="/posts/18c98bc7/" title="深入乱码"><img class="cover" src="/" alt="cover"><div class="content is-center"><div class="date"><i class="anzhiyufont anzhiyu-icon-calendar-days fa-fw"></i> 2023-06-26</div><div class="title">深入乱码</div></div></a></div></div></div><hr><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="anzhiyufont anzhiyu-icon-comments"></i><span> 评论</span></div><div class="comment-randomInfo"><a onclick="anzhiyu.addRandomCommentInfo()" href="javascript:void(0)">匿名评论</a><a href="/privacy" style="margin-left:4px">隐私政策</a></div></div><div class="comment-wrap"><div><div id="waline-wrap"></div></div></div></div><div class="comment-barrage"></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-content"><div class="author-info-avatar"><img class="avatar-img" src="/static/img/head/insectmk.jpg" onerror='this.onerror=null,this.src="/static/img/website/friend_404.gif"' alt="avatar"></div><div class="author-info__description"><div style="line-height:1.38;margin:.6rem 0;text-align:justify;color:rgba(255,255,255,.8)">这有关于<b style="color:#fff">程序开发、部署</b>相关的问题和看法，还有<b style="color:#fff">软件推荐</b>和<b style="color:#fff">分享</b>。</div><div style="line-height:1.38;margin:.6rem 0;text-align:justify;color:rgba(255,255,255,.8)">相信你可以在这里找到对你有用的<b style="color:#fff">知识</b>和<b style="color:#fff">教程</b>。</div></div><div class="author-info__bottom-group"><a class="author-info__bottom-group-left" href="/"><h1 class="author-info__name">InsectMk</h1><div class="author-info__desc">今天特别开心！</div></a><div class="card-info-social-icons is-center"><a class="social-icon faa-parent animated-hover" href="https://gitee.com/insectmk" target="_blank" title="Gitee"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-gitee"></use></svg></a><a class="social-icon faa-parent animated-hover" href="/static/img/website/contact/qq.png" target="_blank" title="QQ"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-qq"></use></svg></a><a class="social-icon faa-parent animated-hover" href="/static/img/website/contact/wechat.png" target="_blank" title="WeChat"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-wechat"></use></svg></a><a class="social-icon faa-parent animated-hover" href="/atom.xml" target="_blank" title="订阅-ATOM"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-rss"></use></svg></a><a class="social-icon faa-parent animated-hover" href="mailto:3067836615@qq.com" target="_blank" title="Email"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-youxiang-"></use></svg></a></div></div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="anzhiyufont anzhiyu-icon-bars"></i><span>文章目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#JDK1-8%E6%96%B0%E7%89%B9%E6%80%A7"><span class="toc-number">1.</span> <span class="toc-text">JDK1.8新特性</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#JDK1-8%E6%96%B0%E7%89%B9%E6%80%A7%E7%AE%80%E4%BB%8B"><span class="toc-number">1.0.1.</span> <span class="toc-text">JDK1.8新特性简介</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="toc-number">1.0.2.</span> <span class="toc-text">Lambda表达式</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%AE%80%E4%BB%8B"><span class="toc-number">1.0.2.1.</span> <span class="toc-text">简介</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95"><span class="toc-number">1.0.2.2.</span> <span class="toc-text">基础语法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B3%A8%E6%84%8F%E7%82%B9"><span class="toc-number">1.0.2.3.</span> <span class="toc-text">注意点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BB%83%E4%B9%A0"><span class="toc-number">1.0.2.4.</span> <span class="toc-text">练习</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E5%BC%8F%E6%8E%A5%E5%8F%A3"><span class="toc-number">1.0.3.</span> <span class="toc-text">函数式接口</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%AE%80%E4%BB%8B-1"><span class="toc-number">1.0.3.1.</span> <span class="toc-text">简介</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9B%9B%E5%A4%A7%E6%A0%B8%E5%BF%83%E5%87%BD%E6%95%B0%E5%BC%8F%E6%8E%A5%E5%8F%A3"><span class="toc-number">1.0.3.2.</span> <span class="toc-text">四大核心函数式接口</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%E3%80%81%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95%E5%92%8C%E6%95%B0%E7%BB%84%E5%BC%95%E7%94%A8"><span class="toc-number">1.0.4.</span> <span class="toc-text">方法、构造方法和数组引用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%E5%BC%95%E7%94%A8"><span class="toc-number">1.0.4.1.</span> <span class="toc-text">方法引用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95%E5%BC%95%E7%94%A8"><span class="toc-number">1.0.4.2.</span> <span class="toc-text">构造方法引用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E5%BC%95%E7%94%A8"><span class="toc-number">1.0.4.3.</span> <span class="toc-text">数组引用</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Stream"><span class="toc-number">1.0.5.</span> <span class="toc-text">Stream</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%AE%80%E4%BB%8B-2"><span class="toc-number">1.0.5.1.</span> <span class="toc-text">简介</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%89%A7%E8%A1%8C%E6%AD%A5%E9%AA%A4"><span class="toc-number">1.0.5.2.</span> <span class="toc-text">执行步骤</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%9B%E5%BB%BAStream"><span class="toc-number">1.0.5.3.</span> <span class="toc-text">创建Stream</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%AD%E9%97%B4%E6%93%8D%E4%BD%9C-%E7%AD%9B%E9%80%89%E4%B8%8E%E5%88%87%E7%89%87"><span class="toc-number">1.0.5.4.</span> <span class="toc-text">中间操作 - 筛选与切片</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%AD%E9%97%B4%E6%93%8D%E4%BD%9C-%E6%98%A0%E5%B0%84"><span class="toc-number">1.0.5.5.</span> <span class="toc-text">中间操作 - 映射</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%AD%E9%97%B4%E6%93%8D%E4%BD%9C-%E6%8E%92%E5%BA%8F"><span class="toc-number">1.0.5.6.</span> <span class="toc-text">中间操作 - 排序</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BB%88%E6%AD%A2%E6%93%8D%E4%BD%9C-%E5%8C%B9%E9%85%8D%E4%B8%8E%E6%9F%A5%E6%89%BE"><span class="toc-number">1.0.5.7.</span> <span class="toc-text">终止操作 - 匹配与查找</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BB%88%E6%AD%A2%E6%93%8D%E4%BD%9C-%E5%BD%92%E7%BA%A6"><span class="toc-number">1.0.5.8.</span> <span class="toc-text">终止操作 - 归约</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BB%88%E6%AD%A2%E6%93%8D%E4%BD%9C-%E6%94%B6%E9%9B%86"><span class="toc-number">1.0.5.9.</span> <span class="toc-text">终止操作 - 收集</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B9%B6%E8%A1%8C%E6%B5%81%E4%B8%8E%E4%B8%B2%E8%A1%8C%E6%B5%81"><span class="toc-number">1.0.6.</span> <span class="toc-text">并行流与串行流</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Optional"><span class="toc-number">1.0.7.</span> <span class="toc-text">Optional</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8E%A5%E5%8F%A3%E7%9A%84%E9%BB%98%E8%AE%A4%E6%96%B9%E6%B3%95%E4%B8%8E%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95"><span class="toc-number">1.0.8.</span> <span class="toc-text">接口的默认方法与静态方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%97%A5%E6%9C%9F%E7%BB%84%E4%BB%B6"><span class="toc-number">1.0.9.</span> <span class="toc-text">日期组件</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BB%84%E4%BB%B6%E7%AE%80%E4%BB%8B"><span class="toc-number">1.0.9.1.</span> <span class="toc-text">组件简介</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%97%A5%E6%9C%9F%E6%97%B6%E9%97%B4%E7%B1%BB%E3%80%81%E6%97%B6%E9%97%B4%E6%88%B3%E3%80%81%E9%97%B4%E9%9A%94%E7%B1%BB"><span class="toc-number">1.0.9.2.</span> <span class="toc-text">日期时间类、时间戳、间隔类</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%97%A5%E6%9C%9F%E6%97%B6%E9%97%B4%E6%A0%BC%E5%BC%8F%E5%8C%96%E7%B1%BB-DateTimeFormatter"><span class="toc-number">1.0.9.3.</span> <span class="toc-text">日期时间格式化类-DateTimeFormatter</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%97%B6%E9%97%B4%E7%9F%AB%E6%AD%A3%E5%99%A8%E7%B1%BB-TemporalAdjuster"><span class="toc-number">1.0.9.4.</span> <span class="toc-text">时间矫正器类-TemporalAdjuster</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%97%B6%E5%8C%BA%E7%B1%BB"><span class="toc-number">1.0.9.5.</span> <span class="toc-text">时区类</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%87%8D%E5%A4%8D%E6%B3%A8%E8%A7%A3%E5%8F%8A%E7%B1%BB%E5%9E%8B%E6%B3%A8%E8%A7%A3"><span class="toc-number">1.0.10.</span> <span class="toc-text">重复注解及类型注解</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="anzhiyufont anzhiyu-icon-history"></i><span>最近发布</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/posts/98f3f974/" title="美化你的Github个人主页"><img src="https://image.insectmk.cn/hexo-gitee-blog/cover/github-insectmk.jpg" onerror='this.onerror=null,this.src="/static/img/website/404.jpg"' alt="美化你的Github个人主页"></a><div class="content"><a class="title" href="/posts/98f3f974/" title="美化你的Github个人主页">美化你的Github个人主页</a><time datetime="2024-07-30T02:52:23.000Z" title="发表于 2024-07-30 10:52:23">2024-07-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/10d64ca8/" title="我的Hexo博客多主题同时部署的实现思路（Butterfly+安知鱼）"><img src="https://image.insectmk.cn/hexo-gitee-blog/cover/hexo-butterfly.jpg" onerror='this.onerror=null,this.src="/static/img/website/404.jpg"' alt="我的Hexo博客多主题同时部署的实现思路（Butterfly+安知鱼）"></a><div class="content"><a class="title" href="/posts/10d64ca8/" title="我的Hexo博客多主题同时部署的实现思路（Butterfly+安知鱼）">我的Hexo博客多主题同时部署的实现思路（Butterfly+安知鱼）</a><time datetime="2024-07-30T01:30:07.000Z" title="发表于 2024-07-30 09:30:07">2024-07-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/129b7f4a/" title="使用Spring AI对接Ollama搭建自己的智能问答网站"><img src="https://image.insectmk.cn/hexo-gitee-blog/cover/ollama.jpg" onerror='this.onerror=null,this.src="/static/img/website/404.jpg"' alt="使用Spring AI对接Ollama搭建自己的智能问答网站"></a><div class="content"><a class="title" href="/posts/129b7f4a/" title="使用Spring AI对接Ollama搭建自己的智能问答网站">使用Spring AI对接Ollama搭建自己的智能问答网站</a><time datetime="2024-07-20T02:45:24.000Z" title="发表于 2024-07-20 10:45:24">2024-07-20</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/9c83ed78/" title="无需更改源码！让你的Hexo的文章在首页隐藏"><img src="https://image.insectmk.cn/hexo-gitee-blog/cover/hexo-hide-posts.jpg" onerror='this.onerror=null,this.src="/static/img/website/404.jpg"' alt="无需更改源码！让你的Hexo的文章在首页隐藏"></a><div class="content"><a class="title" href="/posts/9c83ed78/" title="无需更改源码！让你的Hexo的文章在首页隐藏">无需更改源码！让你的Hexo的文章在首页隐藏</a><time datetime="2024-07-19T02:22:45.000Z" title="发表于 2024-07-19 10:22:45">2024-07-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/9348d5be/" title="使用Cloudflare加速Docker镜像下载"><img src="https://image.insectmk.cn/hexo-gitee-blog/cover/docker.png" onerror='this.onerror=null,this.src="/static/img/website/404.jpg"' alt="使用Cloudflare加速Docker镜像下载"></a><div class="content"><a class="title" href="/posts/9348d5be/" title="使用Cloudflare加速Docker镜像下载">使用Cloudflare加速Docker镜像下载</a><time datetime="2024-06-13T08:01:10.000Z" title="发表于 2024-06-13 16:01:10">2024-06-13</time></div></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div id="workboard"><div id="runtimeTextTip"></div></div><p id="ghbdages"><a class="github-badge" target="_blank" href="https://hexo.io/" style="margin-inline:5px" data-title="博客框架为Hexo_v7.3.0" title="博客框架为Hexo_v7.3.0"><img src="https://npm.elemecdn.com/anzhiyu-blog@2.1.5/img/badge/Frame-Hexo.svg" alt="博客框架为Hexo_v7.3.0"></a><a class="github-badge" target="_blank" href="https://blog.anheyu.com/" style="margin-inline:5px" data-title="本站使用AnZhiYu主题" title="本站使用AnZhiYu主题"><img src="https://npm.elemecdn.com/anzhiyu-theme-static@1.0.9/img/Theme-AnZhiYu-2E67D3.svg" alt="本站使用AnZhiYu主题"></a></p></div><div id="footer-bar"><div class="footer-bar-links"><div class="footer-bar-left"><div id="footer-bar-tips"><div class="copyright">&copy;2020 - 2024 By <a class="footer-bar-link" href="/" title="InsectMk" target="_blank">InsectMk</a></div></div><div id="footer-type-tips"></div></div><div class="footer-bar-right"><a class="footer-bar-link" target="_blank" rel="noopener" href="https://github.com/anzhiyu-c/hexo-theme-anzhiyu" title="主题">主题</a><a class="footer-bar-link" target="_blank" rel="noopener" href="https://beian.miit.gov.cn/" title="蜀ICP备2023013851号-2">蜀ICP备2023013851号-2</a><a class="footer-bar-link" target="_blank" rel="noopener" href="https://beian.mps.gov.cn/#/query/webSearch?code=51142202000154" title="川公网安备51142202000154号">川公网安备51142202000154号</a></div></div></div></footer></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="sidebar-site-data site-data is-center"><a href="/archives/" title="archive"><div class="headline">文章</div><div class="length-num">121</div></a><a href="/tags/" title="tag"><div class="headline">标签</div><div class="length-num">117</div></a><a href="/categories/" title="category"><div class="headline">分类</div><div class="length-num">14</div></a></div><span class="sidebar-menu-item-title">功能</span><div class="sidebar-menu-item"><a class="darkmode_switchbutton menu-child" href="javascript:void(0);" title="显示模式"><i class="anzhiyufont anzhiyu-icon-circle-half-stroke"></i><span>显示模式</span></a></div><div class="back-menu-list-groups"><div class="back-menu-list-group"><div class="back-menu-list-title">网页</div><div class="back-menu-list"><a class="back-menu-item" target="_blank" rel="noopener" href="https://insectmk.cn/" title="主博客"><img class="back-menu-item-icon" src="/static/img/website/favicon.jpg" alt="主博客"><span class="back-menu-item-text">主博客</span></a><a class="back-menu-item" href="https://insectmk.top/" title="次博客"><img class="back-menu-item-icon" src="/static/img/website/favicon.jpg" alt="次博客"><span class="back-menu-item-text">次博客</span></a></div></div><div class="back-menu-list-group"><div class="back-menu-list-title">项目</div><div class="back-menu-list"><a class="back-menu-item" target="_blank" rel="noopener" href="https://gitee.com/insectmk/chatbot-web" title="智能聊天机器人网站"><img class="back-menu-item-icon" src="https://image.insectmk.cn/hexo-gitee-blog/project/icon/chatbot-web.jpg" alt="智能聊天机器人网站"><span class="back-menu-item-text">智能聊天机器人网站</span></a></div></div></div><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-home"></use></svg><span>主页</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-hand-peace"></use></svg><span>娱乐</span></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/entertainment/gallery/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-images"></use></svg><span>图库</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-comments"></use></svg><span>交流</span></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/contact/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-paper-plane"></use></svg><span>留言板</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/contact/link/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-link"></use></svg><span>友链</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-compass"></use></svg><span>目录</span></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/categories/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-archive"></use></svg><span>分类</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/tags/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-tags"></use></svg><span>标签</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/archives/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-folder-open"></use></svg><span>归档</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-server"></use></svg><span>服务</span></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/server/left4dead2/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-icons-left__dead_"></use></svg><span>求生之路</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/server/minecraft/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-minecraft"></use></svg><span>我的世界</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-clipboard"></use></svg><span>日志</span></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/log/website/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-website-with-different-sections"></use></svg><span>个人网站</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/log/pc/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-desktop"></use></svg><span>个人电脑</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-bars"></use></svg><span>其他</span></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/extend/remind/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-note"></use></svg><span>备忘</span></a></li></ul></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-id-card"></use></svg><span>关于</span></a></div></div><span class="sidebar-menu-item-title">标签</span><div class="card-tags"><div class="item-headline"></div><div class="card-tag-cloud"><a href="/tags/AOP/" style="font-size:.88rem">AOP<sup>1</sup></a><a href="/tags/Butterfly/" style="font-size:.88rem">Butterfly<sup>4</sup></a><a href="/tags/Docker/" style="font-size:.88rem">Docker<sup>6</sup></a><a href="/tags/ElementUI/" style="font-size:.88rem">ElementUI<sup>1</sup></a><a href="/tags/Frp/" style="font-size:.88rem">Frp<sup>1</sup></a><a href="/tags/Hexo/" style="font-size:.88rem;font-weight:500;color:var(--anzhiyu-lighttext)">Hexo<sup>8</sup></a><a href="/tags/IntelliJ-IDEA/" style="font-size:.88rem">IntelliJ IDEA<sup>1</sup></a><a href="/tags/JDK/" style="font-size:.88rem">JDK<sup>1</sup></a><a href="/tags/JWT/" style="font-size:.88rem">JWT<sup>1</sup></a><a href="/tags/Java/" style="font-size:.88rem">Java<sup>35</sup></a><a href="/tags/Junit/" style="font-size:.88rem">Junit<sup>1</sup></a><a href="/tags/Leanote/" style="font-size:.88rem">Leanote<sup>1</sup></a><a href="/tags/Maven/" style="font-size:.88rem">Maven<sup>2</sup></a><a href="/tags/MyBatisPlus/" style="font-size:.88rem">MyBatisPlus<sup>1</sup></a><a href="/tags/MySQL5-7/" style="font-size:.88rem">MySQL5.7<sup>1</sup></a><a href="/tags/Mybatis/" style="font-size:.88rem">Mybatis<sup>1</sup></a><a href="/tags/MybatisPlus/" style="font-size:.88rem">MybatisPlus<sup>1</sup></a><a href="/tags/OSS/" style="font-size:.88rem">OSS<sup>1</sup></a><a href="/tags/Spring/" style="font-size:.88rem">Spring<sup>2</sup></a><a href="/tags/Spring-Boot/" style="font-size:.88rem">SpringBoot<sup>6</sup></a><a href="/tags/Vue2/" style="font-size:.88rem">Vue2<sup>9</sup></a><a href="/tags/Vue3/" style="font-size:.88rem">Vue3<sup>1</sup></a><a href="/tags/Vuetify/" style="font-size:.88rem">Vuetify<sup>1</sup></a><a href="/tags/Vuex/" style="font-size:.88rem">Vuex<sup>1</sup></a><a href="/tags/Waline/" style="font-size:.88rem">Waline<sup>3</sup></a><a href="/tags/mvc/" style="font-size:.88rem">mvc<sup>3</sup></a><a href="/tags/mybatis/" style="font-size:.88rem">mybatis<sup>1</sup></a><a href="/tags/profile/" style="font-size:.88rem">profile<sup>1</sup></a><a href="/tags/spring/" style="font-size:.88rem">spring<sup>5</sup></a><a href="/tags/springboot/" style="font-size:.88rem">springboot<sup>4</sup></a><a href="/tags/transaction/" style="font-size:.88rem">transaction<sup>1</sup></a><a href="/tags/yaml/" style="font-size:.88rem">yaml<sup>2</sup></a><a href="/tags/%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2/" style="font-size:.88rem">个人博客<sup>11</sup></a><a href="/tags/%E4%BA%8B%E5%8A%A1/" style="font-size:.88rem">事务<sup>1</sup></a><a href="/tags/%E5%88%86%E9%A1%B5/" style="font-size:.88rem">分页<sup>1</sup></a><a href="/tags/%E5%89%8D%E7%AB%AF/" style="font-size:.88rem;font-weight:500;color:var(--anzhiyu-lighttext)">前端<sup>1</sup></a><a href="/tags/%E6%9C%8D%E5%8A%A1%E5%99%A8/" style="font-size:.88rem">服务器<sup>20</sup></a><a href="/tags/%E6%AF%95%E4%B8%9A%E8%AE%BE%E8%AE%A1/" style="font-size:.88rem">毕业设计<sup>14</sup></a><a href="/tags/%E7%A6%85%E9%81%93/" style="font-size:.88rem">禅道<sup>1</sup></a><a href="/tags/%E7%BB%BF%E8%89%B2%E8%A7%A3%E5%8E%8B/" style="font-size:.88rem">绿色解压<sup>1</sup></a></div></div><hr></div></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="anzhiyufont anzhiyu-icon-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="anzhiyufont anzhiyu-icon-circle-half-stroke"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="anzhiyufont anzhiyu-icon-arrows-left-right"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="anzhiyufont anzhiyu-icon-gear"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="anzhiyufont anzhiyu-icon-list-ul"></i></button><button id="chat-btn" type="button" title="聊天"><i class="anzhiyufont anzhiyu-icon-comment-sms"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="anzhiyufont anzhiyu-icon-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="anzhiyufont anzhiyu-icon-arrow-up"></i></button></div></div><div id="nav-music"><a id="nav-music-hoverTips" onclick="anzhiyu.musicToggle()" accesskey="m">播放音乐</a><div id="console-music-bg"></div><meting-js id="975968032" server="netease" type="playlist" mutex="true" preload="none" theme="var(--anzhiyu-main)" data-lrctype="0" order="random" volume="0.7"></meting-js></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="anzhiyufont anzhiyu-icon-xmark"></i></button></nav><div class="is-center" id="loading-database"><i class="anzhiyufont anzhiyu-icon-spinner anzhiyu-pulse-icon"></i><span> 数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"></div></div><hr><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div id="rightMenu"><div class="rightMenu-group rightMenu-small"><div class="rightMenu-item" id="menu-backward"><i class="anzhiyufont anzhiyu-icon-arrow-left"></i></div><div class="rightMenu-item" id="menu-forward"><i class="anzhiyufont anzhiyu-icon-arrow-right"></i></div><div class="rightMenu-item" id="menu-refresh"><i class="anzhiyufont anzhiyu-icon-arrow-rotate-right" style="font-size:1rem"></i></div><div class="rightMenu-item" id="menu-top"><i class="anzhiyufont anzhiyu-icon-arrow-up"></i></div></div><div class="rightMenu-group rightMenu-line rightMenuPlugin"><div class="rightMenu-item" id="menu-copytext"><i class="anzhiyufont anzhiyu-icon-copy"></i><span>复制选中文本</span></div><div class="rightMenu-item" id="menu-pastetext"><i class="anzhiyufont anzhiyu-icon-paste"></i><span>粘贴文本</span></div><a class="rightMenu-item" id="menu-commenttext"><i class="anzhiyufont anzhiyu-icon-comment-medical"></i><span>引用到评论</span></a><div class="rightMenu-item" id="menu-newwindow"><i class="anzhiyufont anzhiyu-icon-window-restore"></i><span>新窗口打开</span></div><div class="rightMenu-item" id="menu-copylink"><i class="anzhiyufont anzhiyu-icon-link"></i><span>复制链接地址</span></div><div class="rightMenu-item" id="menu-copyimg"><i class="anzhiyufont anzhiyu-icon-images"></i><span>复制此图片</span></div><div class="rightMenu-item" id="menu-downloadimg"><i class="anzhiyufont anzhiyu-icon-download"></i><span>下载此图片</span></div><div class="rightMenu-item" id="menu-newwindowimg"><i class="anzhiyufont anzhiyu-icon-window-restore"></i><span>新窗口打开图片</span></div><div class="rightMenu-item" id="menu-search"><i class="anzhiyufont anzhiyu-icon-magnifying-glass"></i><span>站内搜索</span></div><div class="rightMenu-item" id="menu-searchBaidu"><i class="anzhiyufont anzhiyu-icon-magnifying-glass"></i><span>百度搜索</span></div><div class="rightMenu-item" id="menu-music-toggle"><i class="anzhiyufont anzhiyu-icon-play"></i><span>播放音乐</span></div><div class="rightMenu-item" id="menu-music-back"><i class="anzhiyufont anzhiyu-icon-backward"></i><span>切换到上一首</span></div><div class="rightMenu-item" id="menu-music-forward"><i class="anzhiyufont anzhiyu-icon-forward"></i><span>切换到下一首</span></div><div class="rightMenu-item" id="menu-music-playlist" onclick="window.open(&quot;https://y.qq.com/n/ryqq/playlist/8802438608&quot;, &quot;_blank&quot;);" style="display:none"><i class="anzhiyufont anzhiyu-icon-radio"></i><span>查看所有歌曲</span></div><div class="rightMenu-item" id="menu-music-copyMusicName"><i class="anzhiyufont anzhiyu-icon-copy"></i><span>复制歌名</span></div></div><div class="rightMenu-group rightMenu-line rightMenuOther"><a class="rightMenu-item menu-link" id="menu-randomPost"><i class="anzhiyufont anzhiyu-icon-shuffle"></i><span>随便逛逛</span></a><a class="rightMenu-item menu-link" href="/categories/"><i class="anzhiyufont anzhiyu-icon-cube"></i><span>博客分类</span></a><a class="rightMenu-item menu-link" href="/tags/"><i class="anzhiyufont anzhiyu-icon-tags"></i><span>文章标签</span></a></div><div class="rightMenu-group rightMenu-line rightMenuOther"><a class="rightMenu-item" id="menu-copy" href="javascript:void(0);"><i class="anzhiyufont anzhiyu-icon-copy"></i><span>复制地址</span></a><a class="rightMenu-item" id="menu-commentBarrage" href="javascript:void(0);"><i class="anzhiyufont anzhiyu-icon-message"></i><span class="menu-commentBarrage-text">关闭热评</span></a><a class="rightMenu-item" id="menu-darkmode" href="javascript:void(0);"><i class="anzhiyufont anzhiyu-icon-circle-half-stroke"></i><span class="menu-darkmode-text">深色模式</span></a><a class="rightMenu-item" id="menu-translate" href="javascript:void(0);"><i class="anzhiyufont anzhiyu-icon-language"></i><span>轉為繁體</span></a></div></div><div id="rightmenu-mask"></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.cbd.int/@fancyapps/ui@5.0.28/dist/fancybox/fancybox.umd.js"></script><script src="https://cdn.cbd.int/instant.page@5.2.0/instantpage.js" type="module"></script><script src="https://cdn.cbd.int/node-snackbar@0.1.16/dist/snackbar.min.js"></script><canvas id="universe"></canvas><script async src="https://npm.elemecdn.com/anzhiyu-theme-static@1.0.0/dark/dark.js"></script><script>var HoldLog=console.log;console.log=function(){};let now1=new Date;queueMicrotask(()=>{function o(){HoldLog.apply(console,arguments)}var c=new Date("04/11/2022 14:03:44"),c=(now1.setTime(now1.getTime()+250),(now1-c)/1e3/60/60/24),c=["欢迎使用安知鱼!","生活明朗, 万物可爱",`
        
       █████╗ ███╗   ██╗███████╗██╗  ██╗██╗██╗   ██╗██╗   ██╗
      ██╔══██╗████╗  ██║╚══███╔╝██║  ██║██║╚██╗ ██╔╝██║   ██║
      ███████║██╔██╗ ██║  ███╔╝ ███████║██║ ╚████╔╝ ██║   ██║
      ██╔══██║██║╚██╗██║ ███╔╝  ██╔══██║██║  ╚██╔╝  ██║   ██║
      ██║  ██║██║ ╚████║███████╗██║  ██║██║   ██║   ╚██████╔╝
      ╚═╝  ╚═╝╚═╝  ╚═══╝╚══════╝╚═╝  ╚═╝╚═╝   ╚═╝    ╚═════╝
        
        `,"已上线",Math.floor(c),"天","©2020 By 安知鱼 V1.6.12"],e=["NCC2-036","调用前置摄像头拍照成功，识别为【小笨蛋】.","Photo captured: ","🤪"];setTimeout(o.bind(console,`
%c${c[0]} %c ${c[1]} %c ${c[2]} %c${c[3]}%c ${c[4]}%c ${c[5]}

%c ${c[6]}
`,"color:#425AEF","","color:#425AEF","color:#425AEF","","color:#425AEF","")),setTimeout(o.bind(console,`%c ${e[0]} %c ${e[1]} %c 
${e[2]} %c
${e[3]}
`,"color:white; background-color:#4fd953","","",'background:url("https://npm.elemecdn.com/anzhiyu-blog@1.1.6/img/post/common/tinggge.gif") no-repeat;font-size:450%')),setTimeout(o.bind(console,"%c WELCOME %c 你好，小笨蛋.","color:white; background-color:#4f90d9","")),setTimeout(console.warn.bind(console,"%c ⚡ Powered by 安知鱼 %c 你正在访问 InsectMk 的博客.","color:white; background-color:#f0ad4e","")),setTimeout(o.bind(console,"%c W23-12 %c 你已打开控制台.","color:white; background-color:#4f90d9","")),setTimeout(console.warn.bind(console,"%c S013-782 %c 你现在正处于监控中.","color:white; background-color:#d9534f",""))})</script><script async src="/anzhiyu/random.js"></script><script async>!function(){var n,r,o,a,i,e=new Date("04/11/2022 14:03:44"),l=new Date;setInterval(()=>{var t;if(l=new Date,i=l.getHours(),t=(l-e)/1e3/60/60/24,n=Math.floor(t),t=(l-e)/1e3/60/60-24*n,r=Math.floor(t),1==String(r).length&&(r="0"+r),t=(l-e)/1e3/60-1440*n-60*r,o=Math.floor(t),1==String(o).length&&(o="0"+o),t=(l-e)/1e3-86400*n-3600*r-60*o,a=Math.round(t),1==String(a).length&&(a="0"+a),document.getElementById("footer")){let e="";e=(i<18&&9<=i||null!=(t=document.querySelector("#workboard .workSituationImg"))&&(t.src="",t.title="",t.alt=""),`本站居然运行了 ${n} 天<span id='runtime'> ${r} 小时 ${o} 分 ${a} 秒 </span><i class='anzhiyufont anzhiyu-icon-heartbeat' style='color:red'></i>`),document.getElementById("runtimeTextTip")&&(document.getElementById("runtimeTextTip").innerHTML=e)}},1e3)}()</script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>(()=>{let e=()=>{Waline.init(Object.assign({el:"#waline-wrap",serverURL:"https://waline-server.insectmk.cn/",pageview:!0,dark:'html[data-theme="dark"]',path:window.location.pathname,comment:!0},{reaction:["/static/img/waline/tieba_agree.png","/static/img/waline/tieba_look_down.png","/static/img/waline/tieba_sunglasses.png","/static/img/waline/tieba_pick_nose.png","/static/img/waline/tieba_awkward.png","/static/img/waline/tieba_sleep.png"],requiredMeta:["nick","mail"],locale:{placeholder:"欢迎评论哟~"}}))};var i=async()=>{"object"==typeof Waline||(await getCSS("https://cdn.cbd.int/@waline/client@2.15.5/dist/waline.css"),await getScript("https://cdn.cbd.int/@waline/client@2.15.5/dist/waline.js")),e()};setTimeout(i,0)})()</script><input type="hidden" name="page-type" id="page-type" value="post"></div><script>window.addEventListener("load",()=>{let t=e=>e=""!==e&&150<(e=(e=(e=(e=e.replace(/<img.*?src="(.*?)"?[^\>]+>/gi,"[图片]")).replace(/<a[^>]+?href=["']?([^"']+)["']?[^>]*>([^<]+)<\/a>/gi,"[链接]")).replace(/<pre><code>.*?<\/pre>/gi,"[代码]")).replace(/<[^>]+>/g,"")).length?e.substring(0,150)+"...":e,a=t=>{let a="";if(t.length)for(let e=0;e<t.length;e++)a=(a=(a+="<div class='aside-list-item'>")+`<a href='${t[e].url}' class='thumbnail'><img src='${t[e].avatar}' alt='${t[e].nick}'></a>`)+`<div class='content'>
        <a class='comment' href='${t[e].url}' title='${t[e].content}'>${t[e].content}</a>
        <div class='name'><span>${t[e].nick} / </span><time datetime="${t[e].date}">${anzhiyu.diffDate(t[e].date,!0)}</time></div>
        </div></div>`;else a+="没有评论";var e=document.querySelector("#card-newest-comments .aside-list");e&&(e.innerHTML=a),window.lazyLoadInstance&&window.lazyLoadInstance.update(),window.pjax&&window.pjax.refresh(e)};var e=()=>{var e;document.querySelector("#card-newest-comments .aside-list")&&((e=saveToLocal.get("waline-newest-comments"))?a(JSON.parse(e)):(async()=>{try{var e=(await(await fetch("https://waline-server.insectmk.cn/api/comment?type=recent&count=6",{method:"GET"})).json()).data.map(e=>({content:t(e.comment),avatar:e.avatar,nick:e.nick,url:e.url+"#"+e.objectId,date:e.time||e.insertedAt}));saveToLocal.set("waline-newest-comments",JSON.stringify(e),10/1440),a(e)}catch(e){console.error(e),document.querySelector("#card-newest-comments .aside-list").textContent="无法获取评论，请确认相关配置是否正确"}})())};e(),document.addEventListener("pjax:complete",e)})</script><script>var visitorMail=""</script><script async data-pjax src="https://cdn.cbd.int/anzhiyu-theme-static@1.0.0/waterfall/waterfall.js"></script><script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/qrcodejs/1.0.0/qrcode.min.js"></script><link rel="stylesheet" href="https://cdn.cbd.int/anzhiyu-theme-static@1.1.9/icon/ali_iconfont_css.css"><script async src="//at.alicdn.com/t/c/font_3795172_6hp27xdg2x7.js"></script><script>(()=>{var a,t,h,e;window.ChatraSetup={startHidden:!0},window.chatBtnFn=()=>{document.getElementById("chatra").classList.contains("chatra--expanded")?(Chatra("minimizeWidget"),Chatra("hide")):(Chatra("openChat",!0),Chatra("show"))};a=document,t=window,h="Chatra",t.ChatraID="SjkKhfiLMpdfaPPhc",e=a.createElement("script"),t[h]=t[h]||function(){(t[h].q=t[h].q||[]).push(arguments)},e.async=!0,e.src="https://call.chatra.io/chatra.js",a.head&&a.head.appendChild(e)})()</script><link rel="stylesheet" href="https://cdn.cbd.int/anzhiyu-theme-static@1.0.0/aplayer/APlayer.min.css" media="print" onload='this.media="all"'><script src="https://cdn.cbd.int/anzhiyu-blog-static@1.0.1/js/APlayer.min.js"></script><script src="https://cdn.cbd.int/hexo-anzhiyu-music@1.0.1/assets/js/Meting2.min.js"></script><script src="https://cdn.cbd.int/pjax@0.2.8/pjax.min.js"></script><script>let pjaxSelectors=["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"];var pjax=new Pjax({elements:'a:not([target="_blank"])',selectors:pjaxSelectors,cacheBust:!1,analytics:!1,scrollRestoration:!1});document.addEventListener("pjax:send",function(){if(anzhiyu.removeGlobalFnEvent("pjax"),anzhiyu.removeGlobalFnEvent("themeChange"),document.getElementById("rightside").classList.remove("rightside-show"),window.aplayers)for(let e=0;e<window.aplayers.length;e++)window.aplayers[e].options.fixed||window.aplayers[e].destroy();"object"==typeof typed&&typed.destroy();var e=document.body.classList;e.contains("read-mode")&&e.remove("read-mode")}),document.addEventListener("pjax:complete",function(){window.refreshFn(),document.querySelectorAll("script[data-pjax]").forEach(e=>{let t=document.createElement("script");var a=e.text||e.textContent||e.innerHTML||"";Array.from(e.attributes).forEach(e=>t.setAttribute(e.name,e.value)),t.appendChild(document.createTextNode(a)),e.parentNode.replaceChild(t,e)}),GLOBAL_CONFIG.islazyload&&window.lazyLoadInstance.update(),"function"==typeof panguInit&&panguInit(),"function"==typeof gtag&&gtag("config","",{page_path:window.location.pathname}),"object"==typeof _hmt&&_hmt.push(["_trackPageview",window.location.pathname]),"function"==typeof loadMeting&&document.getElementsByClassName("aplayer").length&&loadMeting(),"object"==typeof Prism&&Prism.highlightAll()}),document.addEventListener("pjax:error",e=>{404===e.request.status&&pjax.loadUrl("/404")})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><script charset="UTF-8" src="https://cdn.cbd.int/anzhiyu-theme-static@1.1.5/accesskey/accesskey.js"></script></div><div id="popup-window"><div class="popup-window-title">通知</div><div class="popup-window-divider"></div><div class="popup-window-content"><div class="popup-tip">你好呀</div><div class="popup-link"><i class="anzhiyufont anzhiyu-icon-arrow-circle-right"></i></div></div></div></body></html>